Skip to content

Adds support for the jsr: protocol #6752

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

Merged
merged 9 commits into from
Apr 8, 2025
Merged

Adds support for the jsr: protocol #6752

merged 9 commits into from
Apr 8, 2025

Conversation

arcanis
Copy link
Member

@arcanis arcanis commented Apr 7, 2025

What's the problem this PR addresses?

I noticed pnpm/pnpm#9358 and figured it'd make sense to support this in Yarn as well.

How did you fix it?

Yarn will natively understand the jsr protocol and automatically reroute the dependency to its @jsr-prefixed variant.

Still need to add tests.

Checklist

  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

@arcanis
Copy link
Member Author

arcanis commented Apr 7, 2025

cc @lucacasonato

@nicolo-ribaudo
Copy link
Contributor

I published https://github.com/nicolo-ribaudo/yarn-plugin-jsr long time ago -- I don't even remember how my plugin works, but I'm sharing it here just in case you notice anything interesting to integrate in Yarn itself :)

Copy link
Contributor

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

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

This is great!

The `jsr:` protocol fetches packages from the [JSR registry](https://jsr.io/).

```
yarn add @luca/flag@jsr:2.0.0
Copy link
Contributor

Choose a reason for hiding this comment

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

pnpm is going with jsr:@luca/[email protected] in the install command. deno add also uses jsr:@luca/[email protected] here. Maybe it's worth aligning?

Copy link
Member Author

@arcanis arcanis Apr 7, 2025

Choose a reason for hiding this comment

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

I don't know for pnpm but in Yarn’s case the syntax here is "standard". By that I mean that we have a data structure to represent a set of packages, called a descriptor.

Descriptors are a combination of package name and an associated range. As a result, their stringified representation is always <name>@<range>, with range being anything (including jsr:<semver> with this PR).

If we were to support yarn add jsr:@foo/bar@... it'd be slightly annoying because it wouldn't be a descriptor: it'd be just a package range for a remapped dependency (ie without a name).

We have some ways to infer a full descriptor from such "nameless" ranges (we do that for yarn add path/to/archive.tgz for example), but it's a little hardcoded at the moment. It'll be fixed in an overhaul I'm working on, but in the meantime I'd prefer to avoid adding new ones.

All that to say, the foo@jsr:1.0.0 syntax is more coherent with how our commands are designed 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok - does this syntax allow importing the latest from JSR? I.e. the equivalent of deno add jsr:@foo/bar? I would like to add this syntax to our docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good question - at the moment the closest would likely be @foo/bar@jsr:*, but this star is a little verbose, doesn't play well with shells, and is saved as-is in the package.json rather than as caret range 🤔

And unfortunately we can't have @foo/bar@jsr:latest, as latest should be interpreted as a package name here for compatibility with other package managers...

I'll think a little more - perhaps we can't avoid having jsr: as a prefix after all? 😔

Copy link
Contributor

Choose a reason for hiding this comment

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

Or add a special yarn jsr add command like in my plugin 😛

Copy link
Member Author

Choose a reason for hiding this comment

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

That'd would be an option too! But a separate command would come with usability issues (would require making two yarn add calls to add dependencies when you start a new project, and would require a separate usage page), so I'd tend to prefer the jsr: prefix. I opened #6757 to this effect.

@arcanis arcanis merged commit 09ce636 into master Apr 8, 2025
26 checks passed
@arcanis arcanis deleted the mael/jsr branch April 8, 2025 07:12
if (ident.scope) {
return `@${scope}/${ident.scope}__${ident.name}`;
} else {
return `@${scope}/${ident.name}`;

Choose a reason for hiding this comment

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

I think JSR does not permit package names without scopes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, but I prefer to treat it as an implementation details, it'd make the code more confusing imo to only support particular codepaths in certain cases.

arcanis added a commit that referenced this pull request Apr 9, 2025
## What's the problem this PR addresses?

My PR at #6752 added support for
the `jsr:` protocol, but in a way that didn't allow for running `yarn
add jsr:@foo/bar` (or even `yarn add @foo/bar@jsr:1.0.0`), because it
was setup as a conversion pass in dependency maps - ie it didn't
integrate well with `suggestUtils`.

## How did you fix it?

I refactored the code to now use a proper resolver & fetcher.

A hack remains (see description of `getResolutionDependencies`); it's a
medium-term solution. I have a better fix in mind, but that will require
some refactoring of our internals so it'll be deferred until the next
major.

## Checklist

<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
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.

4 participants