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 importing absolutely aliased dependency #641

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/ember-auto-import/ts/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,27 @@ export default class Package {
// relative or absolute path, rather than a package name. If the
// originally authored import was an absolute or relative path, it would
// have hit our { type: 'local' } condition before we ran aliasFor.
//
// At the moment, we don't try to handle this case, but we could in the
// future.

packageName = getPackageName(importedPath);

if (packageName) {
let packageRoot: string | undefined;
let packagePath = resolvePackagePath(packageName, this.root);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would be concerned here that if we support aliasing to absolute paths, it should not be mandatory that the package resolves at all.


if (packagePath) {
packageRoot = dirname(packagePath);
}

if (packageRoot) {
return {
type: 'package',
path,
packageName,
packageRoot,
Comment on lines +344 to +345
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps we can alter the types so that in the case of an absolute resolution these are not present. I don't know what the downstream effects of that would be, but they're extremely relevant to this PR, since this PR would be giving that code potentially misleading information. (Nothing enforces that the absolute path is actually coming from this packageName and packageRoot.)

};
}
}

return {
type: 'local',
local: path,
Expand Down
25 changes: 24 additions & 1 deletion test-scenarios/static-import-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ function staticImportTest(project: Project) {
merge(project.files, {
'ember-cli-build.js': `
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const { dirname, join } = require('path');
module.exports = function (defaults) {
let app = new EmberApp(defaults, {
autoImport: {
alias: {
'my-aliased-package': 'original-package'
'my-aliased-package': 'original-package',
'dual-build-package': join(dirname(require.resolve('dual-build-package', { paths: [process.cwd()] })), 'dist/index.mjs'),
},
watchDependencies: [
'original-package'
Expand Down Expand Up @@ -235,6 +237,16 @@ function staticImportTest(project: Project) {
});
});
`,
'absolute-alias-test.js': `
import { module, test } from 'qunit';
import value from 'dual-build-package';

module('Unit | absolute alias', function () {
test('can import package with absolute alias', function (assert) {
assert.equal(value, 'This is ESM');
});
});
`,
'allow-app-imports-test.js': `
import { module, test } from 'qunit';
import example1 from '@ef4/app-template/lib/example1';
Expand Down Expand Up @@ -359,6 +371,17 @@ function staticImportTest(project: Project) {
},
});

project.addDevDependency('dual-build-package', {
files: {
'index.js': `throw new Error('This should not get imported!)`,
dist: {
'index.mjs': `
const value = 'This is ESM';
export default value;`,
},
},
});

project.addDevDependency('@ef4/scoped-lib', {
files: {
'index.js': `
Expand Down
Loading