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

Resolve config from extends package #781

Merged
Merged
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
48 changes: 47 additions & 1 deletion packages/core/__tests__/config/load-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from 'node:fs';
import * as os from 'node:os';
import { describe, beforeEach, afterEach, test, expect } from 'vitest';
import { describe, beforeEach, afterEach, test, expect, vi } from 'vitest';
import { loadConfig } from '../../src/config/index.js';
import { normalizePath } from '../../src/config/config.js';
import { require } from '../../src/config/loader.js';

describe('Config: loadConfig', () => {
const testDir = `${os.tmpdir()}/glint-config-test-load-config-${process.pid}`;
Expand Down Expand Up @@ -51,4 +52,49 @@ describe('Config: loadConfig', () => {
expect(config.environment.getConfiguredTemplateTags()).toEqual({ test: {} });
expect(config.checkStandaloneTemplates).toBe(false);
});

test('locates config in package', () => {
const directory = `${testDir}/package-glint-config`;
const nodeModulePackageDir = `${directory}/node_modules/@package1`;

vi.spyOn(require, 'resolve').mockImplementation((id: string | undefined) => {
if (id === '@package1/tsconfig.json') {
return id.replace('@package1', nodeModulePackageDir);
}
throw Error(`Cannot resolve module ${id}`);
});

fs.mkdirSync(nodeModulePackageDir, { recursive: true });
fs.writeFileSync(
`${nodeModulePackageDir}/package.json`,
JSON.stringify({
name: '@package1',
version: '1.0.0',
})
);
fs.writeFileSync(
`${nodeModulePackageDir}/tsconfig.json`,
JSON.stringify({
glint: {
environment: 'kaboom',
checkStandaloneTemplates: false,
},
})
);
fs.writeFileSync(
`${directory}/tsconfig.json`,
JSON.stringify({
extends: '@package1/tsconfig.json',
glint: {
environment: '../local-env',
Copy link
Contributor

Choose a reason for hiding this comment

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

what's local-env?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest, I simply followed the example from the previous test which uses a predefined environment in the beforeEach

},
})
);

let config = loadConfig(`${directory}`);

expect(config.rootDir).toBe(normalizePath(`${directory}`));
expect(config.environment.getConfiguredTemplateTags()).toEqual({ test: {} });
expect(config.checkStandaloneTemplates).toBe(false);
});
});
23 changes: 20 additions & 3 deletions packages/core/src/config/loader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { createRequire } from 'node:module';
import * as path from 'node:path';
import * as fs from 'node:fs';
import SilentError from 'silent-error';
import { GlintConfig } from './config.js';
import { GlintConfigInput } from '@glint/core/config-types';
import type * as TS from 'typescript';

const require = createRequire(import.meta.url);
/**
* @private
*
* Only exported for testing purposes. Do not import.
*/
export const require = createRequire(import.meta.url);
Copy link
Contributor

Choose a reason for hiding this comment

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

why export this?
conventionally, we really only want to use createRequire in the file it's used in, since the resolution rules are specific to the import.meta.url

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, it's for mocking in tests.

can you add a block-comment with something like:

/**
 * @private 
 *
 * Only exported for testing purposes. Do not import.
 */

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


type TypeScript = typeof TS;

Expand Down Expand Up @@ -75,8 +81,19 @@ function loadConfigInput(ts: TypeScript, entryPath: string): GlintConfigInput |
);

fullGlintConfig = { ...currentGlintConfig, ...fullGlintConfig };
currentPath =
currentContents.extends && path.resolve(path.dirname(currentPath), currentContents.extends);

if (currentContents.extends) {
currentPath = path.resolve(path.dirname(currentPath), currentContents.extends);
if (!fs.existsSync(currentPath)) {
try {
currentPath = require.resolve(currentContents.extends);
} catch (error) {
// control the exception, do nothing.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we log the error?
Or log some message? Could be goofy to debug otherwise?

What's the error thrown here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I log anything this test and others will fail

Please refer to the image below that shows the error being displayed.
Screenshot 2025-01-08 at 15 34 49

Copy link
Contributor

Choose a reason for hiding this comment

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

ahh ok. thanks!

}
}
} else {
currentPath = undefined;
}
}

return validateConfigInput(fullGlintConfig);
Expand Down
Loading