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(unstable): deno add subcommand #22520

Merged
merged 34 commits into from Feb 29, 2024
Merged

Conversation

bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Feb 21, 2024

This commit adds "deno add" subcommand that has a basic support for adding
"jsr:" packages to "deno.json" file.

This currently doesn't support "npm:" specifiers and specifying version constraints.

Screen.Recording.2024-02-21.at.22.07.07.mov

@bartlomieju bartlomieju marked this pull request as draft February 21, 2024 21:30
@marvinhagemeister
Copy link
Contributor

We should also add the related deno remove command which does the opposite and removes the specified identifier from the import map.

@bartlomieju bartlomieju changed the title feat: deno add subcommand feat(unstable): deno add subcommand Feb 28, 2024
@bartlomieju bartlomieju marked this pull request as ready for review February 28, 2024 13:21
Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

Looks good to me, but I think we could use the @scope/pkg/meta.json file to get the latest version so that the CLI doesn't have to depend on another endpoint?

cli/args/flags.rs Outdated Show resolved Hide resolved
tests/integration/pm_tests.rs Outdated Show resolved Hide resolved
cli/tools/registry/pm.rs Outdated Show resolved Hide resolved
Comment on lines 48 to 50
if version_req != "*" {
bail!("Specifying version constraints is currently not supported. Package: {}@{}", jsr_prefixed_name, version_req);
}
Copy link
Member

Choose a reason for hiding this comment

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

It would maybe be nice if the code used the same infrastructure that the jsr specifier completions uses in the lsp (but maybe with cache busting that happens on each run, i forget how it works). Then this would be more easily implemented. Maybe that could be done after #22612 lands

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 used code from @22612, but still left this part. We want to actually compute the best matching semver version here which I believe PackageSearchApi doesn't do.

cli/tools/registry/pm.rs Outdated Show resolved Hide resolved
cli/tools/registry/pm.rs Outdated Show resolved Hide resolved
cli/tools/registry/pm.rs Outdated Show resolved Hide resolved
let mut import_list: Vec<(String, String)> =
existing_imports.into_iter().collect();

import_list.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should sort the user's import list. Instead maybe we should attempt to insert the dependency sorted from bottom up (ex. start at the bottom and keep decrementing the insert index until the previous item is less than the current)? People might have their imports sorted in some semantical way.

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'm not sure it's worth doing for the first pass, it will get gnarly to handle that. Consider scenario like this:

"imports": {
  "@denotest/add": "0.0.1",
  "@foo/bar": "0.5.4",
  "@david/dax": "0.2.1",
  "express": "^4",
  "@david/flag": "0.0.1"
}

And calling deno add @denotest/add @luca/flag @david/dax @std/assert. We'd need to override existing @denotest/add (at first 1) entry then override "@david/dax" (at position 3) and then add "@luca/flag" and "@std/assert" and the end.

Since this is a first pass of the tool I think it's okay - once we have a way to mutate objects in jsonc-parser then we can tackle this in a more sophisticated way. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's do this in a follow-up

cli/tools/registry/pm.rs Outdated Show resolved Hide resolved
tests/util/server/src/servers/registry.rs Outdated Show resolved Hide resolved
Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

LGTM! Will be great to have this command.

cli/tools/registry/api.rs Outdated Show resolved Hide resolved
@@ -32,7 +32,7 @@ mod performance;
mod refactor;
mod registries;
mod repl;
mod search;
pub mod search;
Copy link
Member

Choose a reason for hiding this comment

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

In the future we should move these out of the lsp module to somewhere more common since we're going to be using this for more than the lsp now (cc @nayeemrmn)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like we're just using CliJsrSearchApi::versions() in jsr_find_package_and_select_version. I think we ought to be using JsrResolver::req_to_nv() there or something.. we can modify it to be usable with both fetch or cached-only logic. I'll look into it in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

@nayeemrmn sounds like a good idea. Could you also improve deno add to handle npm: specifiers? For now I left it erroring (there are tests that verify it).

let mut import_list: Vec<(String, String)> =
existing_imports.into_iter().collect();

import_list.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
Copy link
Member

Choose a reason for hiding this comment

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

Ok, let's do this in a follow-up

@bartlomieju bartlomieju enabled auto-merge (squash) February 29, 2024 18:32
@bartlomieju bartlomieju merged commit fb31ae7 into denoland:main Feb 29, 2024
17 checks passed
@bartlomieju bartlomieju deleted the deno_add branch February 29, 2024 19:15
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.

None yet

5 participants