Skip to content

Commit

Permalink
Fix import resolution in ast-utils
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Stevens <jonathan.stevens@eventiva.co.uk>
  • Loading branch information
TGTGamer committed Mar 15, 2024
1 parent 3162606 commit c8c0168
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/schema/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isFromStdlib } from '@zenstackhq/sdk';
import { AstNode, getDocument, LangiumDocuments, Mutable } from 'langium';
import { URI, Utils } from 'vscode-uri';
import { findNodeModulesFile } from './pkg-utils';
import {isAbsolute} from 'node:path'

export function extractDataModelsWithAllowRules(model: Model): DataModel[] {
return model.declarations.filter(
Expand Down Expand Up @@ -96,18 +97,17 @@ export function getDataModelFieldReference(expr: Expression): DataModelField | u

export function resolveImportUri(imp: ModelImport): URI | undefined {
if (!imp.path) return undefined; // This will return true if imp.path is undefined, null, or an empty string ("").

if (!imp.path.endsWith('.zmodel')) {
imp.path += '.zmodel';
}

if (
!imp.path.startsWith('.') // Respect relative paths
&& !imp.path.startsWith('/') // Respect absolute paths (Unix)
&& !/^[a-zA-Z]:\\/.test(imp.path) // Respect absolute paths (Windows)
&& !isAbsolute(imp.path) // Respect Absolute paths
) {
imp.path = findNodeModulesFile(imp.path) ?? imp.path;
}
}

const dirUri = Utils.dirname(getDocument(imp).uri);
return Utils.resolvePath(dirUri, imp.path);
Expand Down

0 comments on commit c8c0168

Please sign in to comment.