Skip to content

Introduce DependencyMap for future import routing#269

Merged
KyrylR merged 1 commit intoBlockstreamResearch:dev/importsfrom
LesterEvSe:feature/dependency-data-structure
Apr 3, 2026
Merged

Introduce DependencyMap for future import routing#269
KyrylR merged 1 commit intoBlockstreamResearch:dev/importsfrom
LesterEvSe:feature/dependency-data-structure

Conversation

@LesterEvSe
Copy link
Copy Markdown
Collaborator

  • Adds the DependencyMap and Remapping data structures.
  • Implements longest-prefix matching to correctly resolve paths.

@LesterEvSe LesterEvSe requested a review from KyrylR April 2, 2026 09:46
@LesterEvSe LesterEvSe self-assigned this Apr 2, 2026
@LesterEvSe LesterEvSe added the enhancement New feature or request label Apr 2, 2026
@LesterEvSe LesterEvSe requested a review from delta1 as a code owner April 2, 2026 09:46
Comment on lines +100 to +112
pub fn test_insert_without_canonicalize(
&mut self,
context: &Path,
dependency_root_path: String,
path: &Path,
) {
self.inner.push(Remapping {
context_prefix: context.to_path_buf(),
dependency_root_path,
target: path.to_path_buf(),
});
self.sort_mappings();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should go to the test mod

// No matches found
Err(Error::UnknownLibrary(first_segment.to_string())).with_span(*use_decl.span())
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have at least a few unit tests

@LesterEvSe LesterEvSe force-pushed the feature/dependency-data-structure branch from 72a8241 to e21af35 Compare April 2, 2026 11:29
@LesterEvSe LesterEvSe requested a review from KyrylR April 2, 2026 11:29
@LesterEvSe LesterEvSe force-pushed the feature/dependency-data-structure branch from e21af35 to 3355d31 Compare April 2, 2026 11:31
Comment on lines +198 to +199
// If your macro generates a different constructor, just swap `From::from`
// with `Identifier::new(s)` or `Identifier(Arc::from(s))`!
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like AI generated comment, let's not include those

Comment on lines +176 to +177
// --- TEST HELPERS ---

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// --- TEST HELPERS ---

/// The base directory that owns this dependency mapping.
pub context_prefix: PathBuf,
/// The name used in the `use` statement (e.g., "math").
pub dependency_root_path: String,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub dependency_root_path: String,
pub drp_name: String,

Comment on lines +208 to +209
// --- TESTS ---

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// --- TESTS ---

@LesterEvSe LesterEvSe force-pushed the feature/dependency-data-structure branch 2 times, most recently from b455b6f to 4199eed Compare April 2, 2026 13:45
@LesterEvSe LesterEvSe requested a review from KyrylR April 2, 2026 13:45
#[derive(Debug, Clone)]
pub struct Remapping {
/// The base directory that owns this dependency mapping.
pub context_prefix: PathBuf,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub context_prefix: PathBuf,
pub context_prefix: CanonPath,

pub fn insert(
&mut self,
context: CanonPath,
dependency_root_path: String,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dependency_root_path: String,
drp_name: String,

Ok(())
}

/// Resolve `use dependency_root_path::...` into a physical file path by finding the
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Resolve `use dependency_root_path::...` into a physical file path by finding the
/// Resolve `use dependency_root_path_name::...` into a physical file path by finding the

@LesterEvSe LesterEvSe force-pushed the feature/dependency-data-structure branch from 4199eed to 683082b Compare April 3, 2026 11:22
@LesterEvSe LesterEvSe requested a review from KyrylR April 3, 2026 11:22
@LesterEvSe LesterEvSe force-pushed the feature/dependency-data-structure branch 2 times, most recently from 37f7b01 to 3695b8a Compare April 3, 2026 11:27
src/parse.rs Outdated

impl UseDecl {
/// Creates a dummy `UseDecl` specifically for testing `DependencyMap` resolution.
/// It automatically builds the internal Identifier path.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// It automatically builds the internal Identifier path.

Comment on lines +61 to +63
/// Returns a `String` containing the OS error if the path does not exist or
/// cannot be accessed. The caller is expected to map this into a more specific
/// compiler diagnostic (e.g., `RichError`).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to block this PR, but the String as an error is too generic, so we will need a follow up PR (after modules are merged into master that would take care of it)

@LesterEvSe could you create a GitHub issue, and link this comment?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +65 to +66
// We use `map_err` here to intercept the generic OS error and enrich
// it with the specific path that failed
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// We use `map_err` here to intercept the generic OS error and enrich
// it with the specific path that failed

Outdated?

Copy link
Copy Markdown
Collaborator Author

@LesterEvSe LesterEvSe Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it's still necessary

Comment on lines +182 to +189
// Check if the current file is executing inside the context's directory tree.
// This prevents a file in `/project_a/` from using a dependency meant for `/project_b/`
if !current_file
.as_path()
.starts_with(remapping.context_prefix.as_path())
{
continue;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we create a method inside the CanonPath and add rust doc there?

Comment on lines +173 to +177
// Safely extract the first segment (the dependency root path)
let parts: Vec<&str> = use_decl.path().iter().map(|s| s.as_inner()).collect();
let first_segment = parts.first().copied().ok_or_else(|| {
Error::CannotParse("Empty use path".to_string()).with_span(*use_decl.span())
})?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to the method of UseDecl

}
}

// No matches found
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// No matches found

@LesterEvSe LesterEvSe force-pushed the feature/dependency-data-structure branch from fb99a23 to f609987 Compare April 3, 2026 12:24
Copy link
Copy Markdown
Collaborator

@KyrylR KyrylR left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f609987

@KyrylR KyrylR merged commit f609987 into BlockstreamResearch:dev/imports Apr 3, 2026
11 checks passed
@KyrylR KyrylR mentioned this pull request Apr 3, 2026
6 tasks
@LesterEvSe LesterEvSe deleted the feature/dependency-data-structure branch April 3, 2026 12:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants