Skip to content

Commit

Permalink
fix: improve Astro detection to avoid false warnings (#682)
Browse files Browse the repository at this point in the history
* fix: improve Astro detection to avoid false warnings

* fix: also return not-found if version is undefined

* chore: changeset
  • Loading branch information
Princesseuh authored Dec 6, 2023
1 parent 015a667 commit 598689a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/rich-adults-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/language-server': patch
'astro-vscode': patch
---

Improve detection of Astro in complex monorepos
6 changes: 5 additions & 1 deletion packages/language-server/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,13 @@ export class AstroCheck {
this.ts = this.typescriptPath ? require(this.typescriptPath) : require('typescript');
const tsconfigPath = this.getTsconfig();

const astroInstall = getAstroInstall([this.workspacePath]);
const config: kit.Config = {
languages: {
astro: getLanguageModule(getAstroInstall([this.workspacePath]), this.ts),
astro: getLanguageModule(
typeof astroInstall === 'string' ? undefined : astroInstall,
this.ts
),
svelte: getSvelteLanguageModule(),
vue: getVueLanguageModule(),
},
Expand Down
18 changes: 15 additions & 3 deletions packages/language-server/src/languageServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,28 @@ export const plugin: LanguageServerPlugin = (
resolveConfig(config, ctx) {
config.languages ??= {};
if (ctx) {
const astroInstall = getAstroInstall([ctx.project.rootUri.fsPath]);
const nearestPackageJson = modules.typescript?.findConfigFile(
ctx.project.rootUri.fsPath,
modules.typescript.sys.fileExists,
'package.json'
);

if (!astroInstall) {
const astroInstall = getAstroInstall([ctx.project.rootUri.fsPath], {
nearestPackageJson: nearestPackageJson,
readDirectory: modules.typescript!.sys.readDirectory,
});

if (astroInstall === 'not-found') {
ctx.server.connection.sendNotification(ShowMessageNotification.type, {
message: `Couldn't find Astro in workspace "${ctx.project.rootUri.fsPath}". Experience might be degraded. For the best experience, please make sure Astro is installed into your project and restart the language server.`,
type: MessageType.Warning,
});
}

config.languages.astro = getLanguageModule(astroInstall, modules.typescript!);
config.languages.astro = getLanguageModule(
typeof astroInstall === 'string' ? undefined : astroInstall,
modules.typescript!
);
config.languages.vue = getVueLanguageModule();
config.languages.svelte = getSvelteLanguageModule();
}
Expand Down
42 changes: 40 additions & 2 deletions packages/language-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,44 @@ export interface AstroInstall {
};
}

export function getAstroInstall(basePaths: string[]): AstroInstall | undefined {
export function getAstroInstall(
basePaths: string[],
checkForAstro?: {
nearestPackageJson: string | undefined;
readDirectory: typeof import('typescript/lib/tsserverlibrary.js').sys.readDirectory;
}
): AstroInstall | 'not-an-astro-project' | 'not-found' {
let astroPath;
let version;

if (checkForAstro && checkForAstro.nearestPackageJson) {
basePaths.push(path.dirname(checkForAstro.nearestPackageJson));

let deps: Set<string> = new Set();
try {
const packageJSON = require(checkForAstro.nearestPackageJson);
[
...Object.keys(packageJSON.dependencies ?? {}),
...Object.keys(packageJSON.devDependencies ?? {}),
...Object.keys(packageJSON.peerDependencies ?? {}),
].forEach((dep) => deps.add(dep));
} catch {}

if (!deps.has('astro')) {
const directoryContent = checkForAstro.readDirectory(
path.dirname(checkForAstro.nearestPackageJson),
['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts'],
undefined,
undefined,
1
);

if (!directoryContent.some((file) => path.basename(file).startsWith('astro.config'))) {
return 'not-an-astro-project';
}
}
}

try {
astroPath = getPackagePath('astro', basePaths);

Expand All @@ -39,10 +73,14 @@ export function getAstroInstall(basePaths: string[]): AstroInstall | undefined {
`${basePaths[0]} seems to be an Astro project, but we couldn't find Astro or Astro is not installed`
);

return undefined;
return 'not-found';
}
}

if (!version) {
return 'not-found';
}

let [major, minor, patch] = version.split('.');

if (patch.includes('-')) {
Expand Down

0 comments on commit 598689a

Please sign in to comment.