diff --git a/external-crates/move/crates/move-compiler/src/command_line/compiler.rs b/external-crates/move/crates/move-compiler/src/command_line/compiler.rs index fe0a2d34d8095..038bbd1a1dae3 100644 --- a/external-crates/move/crates/move-compiler/src/command_line/compiler.rs +++ b/external-crates/move/crates/move-compiler/src/command_line/compiler.rs @@ -15,7 +15,7 @@ use crate::{ expansion, hlir, interface_generator, naming, parser, parser::{comments::*, *}, shared::{ - canonicalize, CompilationEnv, Flags, IndexedPackagePath, IndexedVfsPackagePath, + find_filenames, CompilationEnv, Flags, IndexedPackagePath, IndexedVfsPackagePath, NamedAddressMap, NamedAddressMaps, NumericalAddress, PackageConfig, PackagePaths, }, to_bytecode, @@ -23,7 +23,7 @@ use crate::{ unit_test, }; use move_command_line_common::files::{ - extension_equals, find_filenames, MOVE_COMPILED_EXTENSION, MOVE_EXTENSION, SOURCE_MAP_EXTENSION, + MOVE_COMPILED_EXTENSION, MOVE_EXTENSION, SOURCE_MAP_EXTENSION, }; use move_core_types::language_storage::ModuleId as CompiledModuleId; use move_symbol_pool::Symbol; @@ -280,12 +280,17 @@ impl<'a> Compiler<'a> { )> { /// Path relativization after parsing is needed as paths are initially canonicalized when /// converted to virtual file system paths and would show up as absolute in the test output - /// which wouldn't be machine-agnostic. - fn relativize_path(path: Symbol) -> Symbol { + /// which wouldn't be machine-agnostic. We need to relativize using `vfs_root` beacuse it + /// was also used during canonicalization and might have altered path prefix in a + /// non-standard way (e.g., this can happen on Windows). + fn relativize_path(vsf_root: &VfsPath, path: Symbol) -> Symbol { let Some(current_dir) = std::env::current_dir().ok() else { return path; }; - let Some(new_path) = diff_paths(path.to_string(), current_dir) else { + let Ok(current_dir_vfs) = vsf_root.join(¤t_dir.to_string_lossy()) else { + return path; + }; + let Some(new_path) = diff_paths(path.to_string(), current_dir_vfs.as_str()) else { return path; }; Symbol::from(new_path.to_string_lossy().to_string()) @@ -345,7 +350,7 @@ impl<'a> Compiler<'a> { source_text .iter_mut() - .for_each(|(_, (path, _))| *path = relativize_path(*path)); + .for_each(|(_, (path, _))| *path = relativize_path(&vfs_root, *path)); let res: Result<_, Diagnostics> = SteppedCompiler::new_at_parser(compilation_env, pre_compiled_lib, pprog) @@ -758,18 +763,17 @@ pub fn generate_interface_files( } in other_file_locations { v.extend( - find_filenames(&[path.as_str()], |path| { - extension_equals(path, MOVE_COMPILED_EXTENSION) + find_filenames(&[path], |path| { + path.extension() + .map(|e| e.as_str() == MOVE_COMPILED_EXTENSION) + .unwrap_or(false) })? .into_iter() - .map(|p| { - Ok(IndexedVfsPackagePath { - package, - path: path.root().join(p)?, - named_address_map, - }) - }) - .collect::, anyhow::Error>>()?, + .map(|path| IndexedVfsPackagePath { + package, + path, + named_address_map, + }), ); } v @@ -824,7 +828,7 @@ pub fn generate_interface_files( .to_string_lossy() .to_string(), ); - let vfs_path = deps_out_vfs.join(canonicalize(file_path.as_str().to_owned()))?; + let vfs_path = deps_out_vfs.join(file_path)?; vfs_path.parent().create_dir_all()?; vfs_path .create_file()? diff --git a/external-crates/move/crates/move-compiler/src/parser/mod.rs b/external-crates/move/crates/move-compiler/src/parser/mod.rs index af72faeafe8b3..8f5b4c738ee14 100644 --- a/external-crates/move/crates/move-compiler/src/parser/mod.rs +++ b/external-crates/move/crates/move-compiler/src/parser/mod.rs @@ -13,11 +13,11 @@ pub(crate) mod verification_attribute_filter; use crate::{ diagnostics::FilesSourceText, parser::{self, ast::PackageDefinition, syntax::parse_file_string}, - shared::{CompilationEnv, IndexedVfsPackagePath, NamedAddressMaps}, + shared::{find_move_filenames, CompilationEnv, IndexedVfsPackagePath, NamedAddressMaps}, }; use anyhow::anyhow; use comments::*; -use move_command_line_common::files::{find_move_filenames, FileHash}; +use move_command_line_common::files::FileHash; use move_symbol_pool::Symbol; use std::collections::{BTreeSet, HashMap}; use vfs::VfsPath; @@ -41,18 +41,13 @@ pub(crate) fn parse_program( named_address_map: named_address_mapping, } in paths_with_mapping { - res.extend( - find_move_filenames(&[path.as_str()], true)? - .into_iter() - .map(|s| { - Ok(IndexedVfsPackagePath { - package, - path: path.root().join(s)?, - named_address_map: named_address_mapping, - }) - }) - .collect::, anyhow::Error>>()?, - ); + res.extend(find_move_filenames(&[path], true)?.into_iter().map(|s| { + IndexedVfsPackagePath { + package, + path: s, + named_address_map: named_address_mapping, + } + })); } // sort the filenames so errors about redefinitions, or other inter-file conflicts, are // deterministic diff --git a/external-crates/move/crates/move-compiler/src/shared/mod.rs b/external-crates/move/crates/move-compiler/src/shared/mod.rs index d4f8613421efa..9990d77443084 100644 --- a/external-crates/move/crates/move-compiler/src/shared/mod.rs +++ b/external-crates/move/crates/move-compiler/src/shared/mod.rs @@ -882,16 +882,16 @@ pub struct IndexedVfsPackagePath { pub named_address_map: NamedAddressMapIndex, } -// we need to canonicalized paths for virtual file systems as some of them (e.g., implementation -// of the physical one) cannot handle relative paths -pub fn canonicalize(p: String) -> String { - match std::fs::canonicalize(&p) { - Ok(s) => s.to_string_lossy().to_string(), - Err(_) => p, +pub fn vfs_path_from_str(path: String, vfs_path: &VfsPath) -> Result { + // we need to canonicalized paths for virtual file systems as some of them (e.g., implementation + // of the physical one) cannot handle relative paths + fn canonicalize(p: String) -> String { + match dunce::canonicalize(&p) { + Ok(s) => s.to_string_lossy().to_string(), + Err(_) => p, + } } -} -pub fn vfs_path_from_str(path: String, vfs_path: &VfsPath) -> Result { vfs_path.join(canonicalize(path)) }