1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

v.util: cleanup util.path_styled_for_error_messages/1

This commit is contained in:
Delyan Angelov 2022-12-02 11:41:00 +02:00
parent c77344e0a2
commit 675c362f57
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED

View File

@ -75,22 +75,36 @@ fn color(kind string, msg string) string {
const normalised_workdir = os.wd_at_startup.replace('\\', '/') + '/'
// path_styled_for_error_messages returns the modified file path according
// to the user's preference (`VERROR_PATHS` env-var)
pub fn path_styled_for_error_messages(path_ string) string {
mut path := path_
verror_paths_override := os.getenv('VERROR_PATHS')
if verror_paths_override == 'absolute' {
path = os.real_path(path)
} else {
// always use `/` in the error paths, to ensure the compiler output does not vary in the tests:
path = path.replace('\\', '/')
if path.starts_with(util.normalised_workdir) {
// Get a relative path to the compiler's workdir, when possible:
path = path.replace_once(util.normalised_workdir, '')
const verror_paths_absolute = os.getenv('VERROR_PATHS') == 'absolute'
// path_styled_for_error_messages converts the given file `path`, into one suitable for displaying
// in error messages, produced by the V compiler.
//
// When the file path is prefixed by the working folder, usually that means, that the resulting
// path, will be relative to the current working folder. Relative paths are shorter and stabler,
// because they only depend on the project, and not on the parent folders.
// If the current working folder of the compiler is NOT a prefix of the given path, then this
// function will return an absolute path isntead. Absolute paths are longer, and platform/user
// but they have the advantage of being more easily processible by tools on the same machine.
//
// The V user can opt out of that relativisation, by setting the environment variable VERROR_PATHS,
// to `absolute`. That is useful for starting the V compiler from an IDE or another program, where
// the concept of a "current working folder", is not as clear as working manually with the compiler
// in a shell. By setting VERROR_PATHS=absolute, the IDE/editor can ensure, that the produced error
// messages will have file locations that are easy to find and jump to locally.
//
// NOTE: path_styled_for_error_messages will *always* use `/` in the error paths, no matter the OS,
// to ensure stable compiler error output in the tests.
pub fn path_styled_for_error_messages(path string) string {
mut rpath := os.real_path(path)
rpath = rpath.replace('\\', '/')
if util.verror_paths_absolute {
return rpath
}
if rpath.starts_with(util.normalised_workdir) {
rpath = rpath.replace_once(util.normalised_workdir, '')
}
return path
return rpath
}
// formatted_error - `kind` may be 'error' or 'warn'