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(envs-loading) - fix loading an env with external (non core) env from global scope #8786

Merged
merged 2 commits into from
Apr 15, 2024
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
35 changes: 28 additions & 7 deletions scopes/harmony/aspect-loader/aspect-loader.main.runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, resolve } from 'path';
import { join, resolve, extname } from 'path';
import { NativeCompileCache } from '@teambit/toolbox.performance.v8-cache';
import esmLoader from '@teambit/node.utils.esm-loader';
// import findRoot from 'find-root';
Expand Down Expand Up @@ -538,15 +538,36 @@ export class AspectLoaderMain {
return !isEmpty(files);
}

private searchDistFile(rootDir: string, relativePath: string) {
const defaultDistDir = join(rootDir, 'dist');
const fileExtension = extname(relativePath);
const fileNames = ['ts', 'js', 'tsx', 'jsx'].map((ext) =>
relativePath.replace(new RegExp(`${fileExtension}$`), `.${ext}`)
);
const defaultDistPath = fileNames.map((fileName) => join(defaultDistDir, fileName));
const found = defaultDistPath.find((distPath) => existsSync(distPath));
return found;
}

pluginFileResolver(component: Component, rootDir: string) {
return (relativePath: string) => {
const compiler = this.getCompiler(component);
if (!compiler) {
return join(rootDir, relativePath);
}
try {
const compiler = this.getCompiler(component);
if (!compiler) {
const distFile = this.searchDistFile(rootDir, relativePath);
return distFile || join(rootDir, relativePath);
}

const dist = compiler.getDistPathBySrcPath(relativePath);
return join(rootDir, dist);
const dist = compiler.getDistPathBySrcPath(relativePath);
return join(rootDir, dist);
} catch (err) {
// This might happen for example when loading an env from the global scope, and the env of the env / aspect is not a core one
this.logger.info(
`pluginFileResolver: got an error during get compiler for component ${component.id.toString()}, probably the env is not loaded yet ${err}`
);
const distFile = this.searchDistFile(rootDir, relativePath);
return distFile || join(rootDir, relativePath);
}
};
}

Expand Down
22 changes: 19 additions & 3 deletions scopes/scope/scope/scope-aspects-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Component, ComponentID, LoadAspectsOptions, ResolveAspectsOptions } fro
import { Logger } from '@teambit/logger';
import { EnvsMain } from '@teambit/envs';
import { NodeLinker } from '@teambit/dependency-resolver';
import { BitError } from '@teambit/bit-error';
import { ScopeMain } from './scope.main.runtime';

type ManifestOrAspect = ExtensionManifest | Aspect;
Expand Down Expand Up @@ -225,15 +226,28 @@ needed-for: ${neededFor || '<unknown>'}`);
}

private async compileIfNoDist(capsule: Capsule, component: Component) {
const env = this.envs.getEnv(component);
const compiler: Compiler = env.env.getCompiler();
let compiler: Compiler | undefined;
try {
const env = this.envs.getEnv(component);
compiler = env.env.getCompiler();
} catch (err: any) {
this.logger.info(
`compileIfNoDist: failed loading compiler for ${component.id.toString()} in capsule ${capsule.path}, error: ${
err.message
}`
);
}
const distDir = compiler?.distDir || DEFAULT_DIST_DIRNAME;
const distExists = existsSync(join(capsule.path, distDir));
if (distExists) return;
if (!compiler) {
throw new BitError(`unable to compile aspect/env ${component.id.toString()}, no compiler found`);
}

const compiledCode = (
await Promise.all(
component.filesystem.files.flatMap(async (file) => {
// @ts-ignore - we know it's not null, we have throw error above if yes
if (!compiler.isFileSupported(file.path)) {
return [
{
Expand All @@ -242,8 +256,9 @@ needed-for: ${neededFor || '<unknown>'}`);
},
] as TranspileFileOutputOneFile[];
}

// @ts-ignore - we know it's not null, we have throw error above if yes
if (compiler.transpileFile) {
// @ts-ignore - we know it's not null, we have throw error above if yes
return compiler.transpileFile(file.contents.toString('utf8'), {
filePath: file.path,
componentDir: capsule.path,
Expand All @@ -257,6 +272,7 @@ needed-for: ${neededFor || '<unknown>'}`);

await Promise.all(
compact(compiledCode).map((compiledFile) => {
// @ts-ignore - we know it's not null, we have throw error above if yes
const path = compiler.getDistPathBySrcPath(compiledFile.outputPath);
return capsule?.outputFile(path, compiledFile.outputText);
})
Expand Down