Skip to content

fix(cli): correctly resolve file with fake extension #3924

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
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
27 changes: 17 additions & 10 deletions packages/cli/lib/services/compile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,33 @@ function compileImportedFile({

for (const importedFile of importedFiles) {
const importedFilePath = path.resolve(path.dirname(filePath), importedFile);
const importedFilePathWithoutExtension = path.join(path.dirname(importedFilePath), path.basename(importedFilePath, path.extname(importedFilePath)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't follow where the bug is. path.basename('a/b/c/schema.zod.ts', path.extname('ts'))) should give a/b/c/schema.zod no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's because extension-less imports are valid in Typescript, so when you autocomplete, it often imports this schema.zod and since we are less smart, we think .zod is the extension.

Copy link
Collaborator

Choose a reason for hiding this comment

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

got it

const importedFilePathWithExtension = importedFilePathWithoutExtension + '.ts';
let importedFilePathResolved: string;
if (importedFilePath.endsWith('.ts')) {
importedFilePathResolved = importedFilePath;
} else if (importedFilePath.endsWith('.js')) {
importedFilePathResolved = `${path.join(path.dirname(importedFilePath), path.basename(importedFilePath, path.extname(importedFilePath)))}.ts`;
} else {
importedFilePathResolved = `${importedFilePath}.ts`;
}

/// if it is a library import then we can skip it
if (!fs.existsSync(importedFilePathWithExtension)) {
if (!fs.existsSync(importedFilePathResolved)) {
// if the library is not allowed then we should let the user know
// that it is not allowed and won't work early on
if (!ALLOWED_IMPORTS.includes(importedFile)) {
console.log(chalk.red(`Importing libraries is not allowed. Please remove the import "${importedFile}" from "${path.basename(filePath)}"`));
return false;
}

// if it is a library import then we can skip it
continue;
}

// if the file is not in the nango-integrations directory
// then we should not compile it
// if the parts of the path are shorter than the current that means it is higher
// than the nango-integrations directory
if (importedFilePathWithExtension.split(path.sep).length < fullPath.split(path.sep).length) {
const importedFileName = path.basename(importedFilePathWithExtension);
if (importedFilePathResolved.split(path.sep).length < fullPath.split(path.sep).length) {
const importedFileName = path.basename(importedFilePathResolved);

console.log(
chalk.red(
Expand All @@ -199,14 +206,14 @@ function compileImportedFile({
return false;
}

if (importedFilePathWithExtension.includes('models.ts')) {
if (importedFilePathResolved.includes('models.ts')) {
continue;
}

compiler.compile(fs.readFileSync(importedFilePathWithExtension, 'utf8'), importedFilePathWithExtension);
console.log(chalk.green(`Compiled "${importedFilePathWithExtension}" successfully`));
compiler.compile(fs.readFileSync(importedFilePathResolved, 'utf8'), importedFilePathResolved);
console.log(chalk.green(`Compiled "${importedFilePathResolved}" successfully`));

finalResult = compileImportedFile({ fullPath, filePath: importedFilePathWithExtension, compiler, type, parsed });
finalResult = compileImportedFile({ fullPath, filePath: importedFilePathResolved, compiler, type, parsed });
}

return finalResult;
Expand Down
Loading