Skip to content

Commit

Permalink
fix: issue with default compiler not being found
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 18, 2023
1 parent 70ea6ca commit 4ab546c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
8 changes: 8 additions & 0 deletions .changeset/rotten-snakes-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/language-server": patch
"@marko/language-tools": patch
"marko-vscode": patch
"@marko/type-check": patch
---

Fix issue with default compiler not being found.
7 changes: 7 additions & 0 deletions packages/language-server/src/ts-plugin/host.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from "path";
import type ts from "typescript/lib/tsserverlibrary";
import * as defaultCompiler from "@marko/compiler";
import * as defaultConfig from "@marko/compiler/config";
import * as defaultTranslator from "@marko/translator-default";
import {
type Extracted,
Processors,
Expand All @@ -14,6 +17,10 @@ Project.setDefaultTypePaths({
internalTypesFile: path.join(__dirname, "marko.internal.d.ts"),
markoTypesFile: path.join(__dirname, "marko.runtime.d.ts"),
});
Project.setDefaultCompilerMeta(defaultCompiler, {
...defaultConfig,
translator: defaultTranslator,
});

export interface ExtractedSnapshot extends Extracted {
snapshot: ts.IScriptSnapshot;
Expand Down
43 changes: 36 additions & 7 deletions packages/language-tools/src/util/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import stripJSONComments from "strip-json-comments";
import { ScriptLang } from "../extractors/script";

export interface Meta {
fallback: boolean;
compiler: typeof Compiler;
config: Omit<Compiler.Config, "cache" | "translator"> & {
cache: Map<any, any>;
Expand All @@ -27,6 +26,7 @@ interface TypeLibs {
}

const defaultTypeLibs: Partial<TypeLibs> = {};
let defaultMeta: Meta | undefined;
const ignoreErrors = (_err: Error) => {};
const metaByDir = new Map<string, Meta>();
const metaByCompiler = new Map<string, Meta>();
Expand Down Expand Up @@ -178,6 +178,10 @@ export function getScriptLang(
}

export function clearCaches() {
if (defaultMeta) {
clearCacheForMeta(defaultMeta);
}

for (const project of metaByCompiler.values()) {
clearCacheForMeta(project);
}
Expand All @@ -187,11 +191,38 @@ export function setDefaultTypePaths(defaults: typeof defaultTypeLibs) {
Object.assign(defaultTypeLibs, defaults);
}

function getMeta(dir = __dirname): Meta {
export function setDefaultCompilerMeta(
compiler: typeof Compiler,
config: Compiler.Config
) {
const { translator } = config;
if (typeof translator !== "object") {
throw new Error("Translator must be fully resolved and loaded.");
}

defaultMeta = {
compiler,
config: {
...config,
cache: new Map(),
translator,
},
};
}

function getMeta(dir?: string): Meta {
if (!dir) {
if (!defaultMeta) {
throw new Error(
"@marko/compiler must be installed or compiler registered."
);
}

return defaultMeta;
}

let cached = metaByDir.get(dir);
if (!cached) {
const fallback = dir === __dirname;

try {
const require = createRequire(dir);
const configPath = require.resolve("@marko/compiler/config");
Expand All @@ -203,7 +234,6 @@ function getMeta(dir = __dirname): Meta {
)) as typeof Compiler;
const config = interopDefault(require(configPath)) as Compiler.Config;
cached = {
fallback,
compiler,
config: {
...config,
Expand All @@ -215,7 +245,6 @@ function getMeta(dir = __dirname): Meta {
metaByCompiler.set(configPath, cached);
}
} catch (err) {
if (fallback) throw err;
cached = getMeta();
}

Expand All @@ -240,7 +269,7 @@ function getTagLookupForProject(meta: Meta, dir: string): TaglibLookup {
ignoreErrors
);
} catch {
if (!meta.fallback) {
if (meta !== defaultMeta) {
lookup = getTagLookupForProject(getMeta(), dir);
}
}
Expand Down

0 comments on commit 4ab546c

Please sign in to comment.