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

feat: add npm specifiers to the graph #232

Merged
merged 26 commits into from
Feb 22, 2023

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Feb 14, 2023

We're going to rely on integration tests in the CLI for this atm (opened #241 for the future).

Closes #230

@dsherret dsherret marked this pull request as ready for review February 21, 2023 23:47
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM

src/lib.rs Show resolved Hide resolved
src/graph.rs Outdated
Comment on lines 2014 to 2074
for (specifier, npm_ref, maybe_range) in infos {
if !self.graph.module_slots.contains_key(&specifier) {
if let Some(npm_resolver) = &self.npm_resolver {
// todo: cache resolutions to ensure they always resolve the same
let resolution = npm_resolver.resolve_npm(&npm_ref.req);
match resolution {
Ok(pkg_id) => {
specifier_resolutions
.insert(specifier.clone(), pkg_id.clone());
let pkg_id_ref = NpmPackageNvReference {
nv: pkg_id,
sub_path: npm_ref.sub_path,
};
let resolved_specifier = pkg_id_ref.as_specifier();
if resolved_specifier != specifier {
self
.graph
.redirects
.insert(specifier, resolved_specifier.clone());
}
self.graph.module_slots.insert(
resolved_specifier.clone(),
ModuleSlot::Module(Module::Npm(NpmModule {
specifier: resolved_specifier,
nv_reference: pkg_id_ref,
})),
);
}
Err(err) => {
self.graph.module_slots.insert(
specifier.clone(),
ModuleSlot::Err(ModuleGraphError::ResolutionError(
ResolutionError::ResolverError {
error: Arc::new(err),
specifier: specifier.to_string(),
// this should always be set,
range: maybe_range.unwrap_or_else(|| Range {
specifier,
start: Position::zeroed(),
end: Position::zeroed(),
}),
},
)),
);
}
}
} else {
self.graph.module_slots.insert(
specifier.clone(),
ModuleSlot::Err(ModuleGraphError::LoadingErr(
specifier,
maybe_range,
Arc::new(anyhow::anyhow!(
"npm specifiers are not supported in this environment"
)),
)),
);
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This is very nice, consider factoring the whole body of while into a helper function, could be much easier to grep with fewer indents

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about that, but got lazy. Will update soon

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a lot of work to do this properly. I'm going to skip on this for now and add a todo

src/graph.rs Outdated Show resolved Hide resolved
@dsherret dsherret merged commit ed58994 into denoland:main Feb 22, 2023
@dsherret dsherret deleted the feat_npm_specifiers_graph branch February 22, 2023 01:42
dsherret added a commit to denoland/deno that referenced this pull request Feb 22, 2023
This changes npm specifiers to be handled by deno_graph and resolved to
an npm package name and version when the specifier is encountered. It
also slightly changes how npm specifier resolution occurs—previously it
would collect all the npm specifiers and resolve them all at once, but
now it resolves them on the fly as they are encountered in the module
graph.

denoland/deno_graph#232

---------

Co-authored-by: Bartek Iwańczuk <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add npm specifiers to the graph
2 participants