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(npm): reload an npm package's dependency's information when version not found #18622

Merged

Conversation

dsherret
Copy link
Member

@dsherret dsherret commented Apr 7, 2023

This reloads an npm package's dependency's information when a version/version req/tag is not found.

This PR applies only to dependencies of npm packages. It does NOT yet cause npm specifiers to have their dependency information cache busted. That requires a different solution, but this should help cache bust in more scenarios.

Part of #16901, but doesn't close it yet

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, this change is so welcome

Comment on lines +1625 to +1671
// should error when `--cache-only` is used now because the version is not in the cache
let output = test_context
.new_command()
.args("run --cached-only main.ts")
.run();
output.assert_exit_code(1);
output.assert_matches_text("error: Could not find npm package '@denotest/cjs-default-export' matching '^1.0.0'.\n");

// now try running without it, it should download the package now
let output = test_context.new_command().args("run main.ts").run();
output.assert_matches_text(concat!(
"Download https://localhost:4545/npm/registry/@denotest/esm-import-cjs-default\n",
"Download https://localhost:4545/npm/registry/@denotest/cjs-default-export\n",
"Node esm importing node cjs\n[WILDCARD]",
));
output.assert_exit_code(0);

// now remove the information for the top level package
let registry_json_path = "npm/localhost_4545/npm/registry/@denotest/esm-import-cjs-default/registry.json";
let mut registry_json: Value =
serde_json::from_str(&deno_dir.read_to_string(registry_json_path)).unwrap();
remove_version(&mut registry_json, "1.0.0");
deno_dir.write(
registry_json_path,
serde_json::to_string(&registry_json).unwrap(),
);

// should error again for --cached-only
let output = test_context
.new_command()
.args("run --cached-only main.ts")
.run();
output.assert_exit_code(1);
output.assert_matches_text(concat!(
"error: Could not find npm package '@denotest/esm-import-cjs-default' matching '1.0.0'. Try retrieving the latest npm package information by running with --reload\n",
" at file:https:///[WILDCARD]/main.ts:1:8\n",
));

// now try running without it, it currently will error, but this should work in the future
// todo(https://github.com/denoland/deno/issues/16901): fix this
let output = test_context.new_command().args("run main.ts").run();
output.assert_exit_code(1);
output.assert_matches_text(concat!(
"error: Could not find npm package '@denotest/esm-import-cjs-default' matching '1.0.0'. Try retrieving the latest npm package information by running with --reload\n",
" at file:https:///[WILDCARD]/main.ts:1:8\n",
));
}
Copy link
Member

Choose a reason for hiding this comment

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

Great test, I really like the reviewing them with the new test infrastructure. So much easier to understand without the boilerplate.

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 less arduous to write too!

@@ -188,7 +191,7 @@ impl NpmResolution {
pub fn resolve_pkg_id_from_deno_module(
&self,
id: &NpmPackageNv,
) -> Result<NpmPackageId, AnyError> {
) -> Result<NpmPackageId, PackageNvNotFoundError> {
Copy link
Member

Choose a reason for hiding this comment

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

Slowly grinding to strongly typed errors 💪

match package_info.version_info(&nv) {
Ok(version_info) => Ok(version_info),
Err(err) => {
bail!("Could not find '{}' specified in the lockfile. Maybe try again with --reload", err.0);
Copy link
Member Author

@dsherret dsherret Apr 7, 2023

Choose a reason for hiding this comment

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

Oh, we should update this to also not require --reload. I will do it in a future PR. (edit: opened #18624)

@dsherret dsherret merged commit 5c7f76c into denoland:main Apr 7, 2023
@dsherret dsherret deleted the fix_reload_package_information_not_found branch April 7, 2023 01:41
dsherret added a commit that referenced this pull request Apr 12, 2023
Part 1: #18622 
Part 2: This PR

Closes #16901

---------

Co-authored-by: Luca Casonato <[email protected]>
levex pushed a commit that referenced this pull request Apr 12, 2023
…on not found (#18622)

This reloads an npm package's dependency's information when a
version/version req/tag is not found.

This PR applies only to dependencies of npm packages. It does NOT yet
cause npm specifiers to have their dependency information cache busted.
That requires a different solution, but this should help cache bust in
more scenarios.

Part of #16901, but doesn't close it yet
levex pushed a commit that referenced this pull request Apr 12, 2023
Part 1: #18622 
Part 2: This PR

Closes #16901

---------

Co-authored-by: Luca Casonato <[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.

2 participants