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

Self imports are not working #86

Open
piranna opened this issue Mar 1, 2023 · 7 comments · Fixed by #87
Open

Self imports are not working #86

piranna opened this issue Mar 1, 2023 · 7 comments · Fixed by #87

Comments

@piranna
Copy link

piranna commented Mar 1, 2023

When doing a self-import (at least for scoped packages) I get a n/no-missing-import error. I'm doing it for my project tests files, and when debugging it, I've traced it down to

// node_modules/import-meta-resolve/lib/package-json-reader.js
var import_fs = __toESM(require("fs"), 1);
var import_path = __toESM(require("path"), 1);
var reader = { read };
var package_json_reader_default = reader;
function read(jsonPath) {
return find(import_path.default.dirname(jsonPath));
}
function find(dir) {
try {
const string = import_fs.default.readFileSync(import_path.default.toNamespacedPath(import_path.default.join(dir, "package.json")), "utf8");
return { string };
} catch (error) {
if (error.code === "ENOENT") {
const parent = import_path.default.dirname(dir);
if (dir !== parent)
return find(parent);
return { string: void 0 };
}
throw error;
}
}
, where for an input file it's looking it's package.json file and returning its contend when called from
const source = package_json_reader_default.read(path4).string;
. Problem is that at
const packageConfig2 = getPackageConfig(packageJsonPath, specifier, base);
if (packageConfig2.exports !== void 0 && packageConfig2.exports !== null)
return packageExportsResolve(packageJsonUrl, packageSubpath, packageConfig2, base, conditions).resolved;
if (packageSubpath === ".")
return legacyMainResolve(packageJsonUrl, packageConfig2, base);
return new import_url2.URL(packageSubpath, packageJsonUrl);
is being used with the original requested path instead of the package root one, so file resolution fails.

On the other hand, I've found that in fact this file is a transpiled one, probably generated with webpack or something like that, so it's ugly to work with it. Original import-meta-resolve exists, and it's updated from two months ago, so why don't use it instead using a big blob?

@piranna
Copy link
Author

piranna commented Mar 1, 2023

Ok, just by commenting this three lines

      // const parent = import_path.default.dirname(dir);
      // if (dir !== parent)
      //   return find(parent);

that in fact doesn't exists in upstream it gets fixed. Not sure if create a PR removing them, or fully replacing the import-meta-resolve package...

@piranna
Copy link
Author

piranna commented Mar 1, 2023

Properly fixed at #87

@aladdin-add
Copy link

it should be has been fixed in v16.0.0. just leave it open as it still needs some tests.

@piranna
Copy link
Author

piranna commented May 16, 2023

So far version 16.0.2 seems to be working on my side.

@piranna
Copy link
Author

piranna commented May 25, 2023

I think we can close this now.

@voxpelli voxpelli reopened this May 25, 2023
@voxpelli
Copy link
Member

Closed before reading that @aladdin-add wanted to keep it open to add some tests, sorry @aladdin-add 🙏

@scagood
Copy link

scagood commented Nov 26, 2023

I want to be very careful to not break this in #139

Can you provide a simple example of this? (I want to add a test!)

I assume its something like:

// package.json
{ "name": "@org/pkg", "main": "./index.js" }
// index.js
export function something() {
  // magic
}
// some.test.js
import { something } from '@org/pkg';

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 a pull request may close this issue.

4 participants