Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): unexpected StripPrefixError #7146

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/@winglang/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ pub fn find_nearest_wing_project_dir(source_path: &Utf8Path) -> Utf8PathBuf {
return initial_dir;
}

fn common_path(path1: &Utf8Path, path2: &Utf8Path) -> Utf8PathBuf {
assert!(path1.is_absolute(), "path1 must be absolute");
assert!(path2.is_absolute(), "path2 must be absolute");

let components1: Vec<_> = path1.components().collect();
let components2: Vec<_> = path2.components().collect();

let common_prefix = components1
.iter()
.zip(components2.iter())
.take_while(|&(a, b)| a == b)
.map(|(component, _)| component)
.collect::<Vec<_>>();

common_prefix.into_iter().collect()
}

pub fn compile(source_path: &Utf8Path, source_text: Option<String>, out_dir: &Utf8Path) -> Result<CompilerOutput, ()> {
let project_dir = find_nearest_wing_project_dir(source_path);
let source_package = as_wing_library(&project_dir, false).unwrap_or_else(|| DEFAULT_PACKAGE_NAME.to_string());
Expand All @@ -336,7 +353,7 @@ pub fn compile(source_path: &Utf8Path, source_text: Option<String>, out_dir: &Ut

// A map from package names to their root directories
let mut library_roots: IndexMap<String, Utf8PathBuf> = IndexMap::new();
library_roots.insert(source_package, project_dir.to_owned());
library_roots.insert(source_package.clone(), project_dir.to_owned());

// -- PARSING PHASE --
let mut files = Files::new();
Expand All @@ -353,6 +370,22 @@ pub fn compile(source_path: &Utf8Path, source_text: Option<String>, out_dir: &Ut
&mut asts,
);

// If there's no wing.toml or package.json, then we don't know for sure where the root
// of the project is, so we simply find the common root of all files that are part of the
// default package.
if source_package == DEFAULT_PACKAGE_NAME {
let mut common_root = project_dir.to_owned();
for file in &topo_sorted_files {
if file.package != source_package {
continue;
}

common_root = common_path(&common_root, &file.path);
}
// Update the source package to be the common root
library_roots.insert(source_package.clone(), common_root);
}

emit_warning_for_unsupported_package_managers(&project_dir);

// -- DESUGARING PHASE --
Expand Down
65 changes: 48 additions & 17 deletions packages/@winglang/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7496,27 +7496,58 @@ fn lookup_known_type(name: &'static str, env: &SymbolEnv) -> TypeRef {
/// ```
pub fn calculate_fqn_for_namespace(package_name: &str, package_root: &Utf8Path, path: &Utf8Path) -> String {
let normalized_root = normalize_path(&package_root, None);
let normalized = normalize_path(&path, None);
if normalized.starts_with("..") {
panic!(
"File path \"{}\" is not within the package root \"{}\"",
path, package_root
);
let normalized_path = normalize_path(&path, None);
if normalized_path.starts_with("..") {
report_diagnostic(Diagnostic {
message: format!(
"File path \"{}\" is not within the package root \"{}\"",
path, package_root
),
span: None,
annotations: vec![],
hints: vec![],
severity: DiagnosticSeverity::Error,
});
return package_name.to_string();
}
Chriscbr marked this conversation as resolved.
Show resolved Hide resolved

let assembly = package_name;
let normalized = if normalized.as_str().ends_with(".w") {
normalized.parent().expect("Expected a parent directory")
let normalized_dir = if normalized_path.as_str().ends_with(".w") {
if let Some(normalized_parent) = normalized_path.parent() {
normalized_parent
} else {
report_diagnostic(Diagnostic {
message: format!("Source file \"{}\" does not have a parent directory", path),
span: None,
annotations: vec![],
hints: vec![],
severity: DiagnosticSeverity::Error,
});
return package_name.to_string();
}
} else {
&normalized
&normalized_path
};
let relative_path = normalized
.strip_prefix(&normalized_root)
.expect(format!("not a prefix: {} {}", normalized_root, normalized).as_str());
if relative_path == Utf8Path::new("") {
return assembly.to_string();
}
let namespace = relative_path.as_str().replace("/", ".");
format!("{}.{}", assembly, namespace)

if let Ok(relative_path) = normalized_dir.strip_prefix(&normalized_root) {
if relative_path == Utf8Path::new("") {
return assembly.to_string();
}
let namespace = relative_path.as_str().replace("/", ".");
return format!("{}.{}", assembly, namespace);
} else {
report_diagnostic(Diagnostic {
message: format!(
"Source file \"{}\" is not within the package root \"{}\"",
path, package_root
),
span: None,
annotations: vec![],
hints: vec![],
severity: DiagnosticSeverity::Error,
});
return package_name.to_string();
}
}

#[derive(Debug)]
Expand Down
1 change: 1 addition & 0 deletions tests/valid/subdir/bring_outer.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bring "../baz.w" as baz;
Loading