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

Make this work with yarn berry (aka yarn 2/3/4) #26

Closed
francisu opened this issue Mar 13, 2024 · 7 comments
Closed

Make this work with yarn berry (aka yarn 2/3/4) #26

francisu opened this issue Mar 13, 2024 · 7 comments

Comments

@francisu
Copy link

Currently prettier, which uses import-meta-resolve does not work with yarn berry using PnP (the default node linker): yarnpkg/berry#6167.

This effectively breaks sveltkit when used with yarn berry.

I have been able to fix the problem in prettier by changing the importFromFile function as follows:

function importFromFile(specifier, parent) {
  if (process.versions.pnp) {
    const { createRequire } = require('module');
    const targetRequire = createRequire(parent);
    const resolved = targetRequire.resolve(specifier);
    return targetRequire(resolved);
  } else {
    const url2 = resolve2(specifier, pathToFileURL4(parent).href);
    return import(url2);
  }
}

However, the real fix should be implemented in import-meta-resolve rather than prettier (the fix can be essentially the same).

You can easily reproduce the issue using the steps in the above yarn issue.

I'm happy to help with making the fix if it's something you will accept. Currently I have to patch it in prettier since import-meta-resolve is embedded in there distribution.

@wooorm
Copy link
Owner

wooorm commented Mar 13, 2024

Can you check the issues, it's been discussed. It's impossible. Or: feel free to spend like a month on this if you really want it.

Also: I think svelte itself also is like: yeah, no, not going to happen.

@francisu
Copy link
Author

I have made it work with the above code, so it's not impossible. It you can change the resolver to add those few lines of code to detect pnp, then it will work fine. I'm happy to help here.

Old comments about yarn pnp not working with svelte are no longer true, as bugs have been fixed.

@wooorm
Copy link
Owner

wooorm commented Mar 13, 2024

This pkg is a ton of code, it’s a copy of the actual node internals. Just using require isn’t going to work on ESM files for this project. If that’s all your use case, then that’s fine for you?

I meant Rich’s comments on not being interested in supporting PnP

@wooorm
Copy link
Owner

wooorm commented Mar 13, 2024

Please see #23 (comment) and the above and related threads, which include the yarn maintainer

@francisu
Copy link
Author

francisu commented Mar 13, 2024

This pkg is a ton of code, it’s a copy of the actual node internals. Just using require isn’t going to work on ESM files for this project. If that’s all your use case, then that’s fine for you?

This package will only be loaded in the PnP case, so it won't penalize anyone else.

I have not investigated how to do the fix with the ESM portions of your code, as I don't require that, but I'm sure something similar can be done in your code to handle this. If necessary we can get help from the yarn folks like @arcanis who have been very active in helping with exactly these situation. I'm willing to help too.

As I said before, I have resolved my problem by patching Prettier and bypassing the call to this package, but I'm trying to advocate for a broader fix which will help more people.

I meant Rich’s comments on not being interested in supporting PnP

Yes, I have seen this. I find it not very useful or helpful. Svelte does not need to do anything directly to support PnP, the issues are with underlying packages (like this one). His comment is also quite old. yarn pnp is very actively developed and constantly improving.

@wooorm
Copy link
Owner

wooorm commented Apr 3, 2024

Ok well #23 (comment) still stands. I think it’s going to take several weeks for someone to implement. It would be a fork of this project or a completely different project. If someone wants to make that, we can talk again after that.
It basically means copy/pasting a lot of Node into a project. At that point, using Node itself seems simpler to me.

@wooorm wooorm closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2024
francisu added a commit to snapstrat/import-meta-resolve that referenced this issue Jun 1, 2024
@francisu
Copy link
Author

francisu commented Jun 1, 2024

See PR #28

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

No branches or pull requests

2 participants