Skip to content

Commit e35de77

Browse files
committed
Fix windows require bug
Signed-off-by: WolfGangS <flamin2k8@gmail.com>
1 parent e163813 commit e35de77

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/shared/includeprocessor.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export class IncludeProcessor {
129129
let aliased = false;
130130

131131
if(isRequire) {
132+
filename = this.normalizeRequirePath(filename);
132133
if(!filename.startsWith("@")) {
133134
// Regular require, relative lookup
134135
includePaths = ["."];
@@ -324,6 +325,15 @@ export class IncludeProcessor {
324325
};
325326
}
326327

328+
private normalizeRequirePath(filename: string): string {
329+
if(!filename.includes(path.sep)) {
330+
filename = filename.split("/").join(path.sep);
331+
}
332+
const dbl = path.sep + path.sep;
333+
while(filename.includes(dbl)) filename = filename.split(dbl).join(path.sep);
334+
return filename;
335+
}
336+
327337
private async getLuauRequireAliasDir(requirePath:string, sourceFile:NormalizedPath, state:IncludeState) : Promise<string> {
328338
if(!requirePath.startsWith("@")) {
329339
throw "Alias must start with @";
@@ -353,9 +363,13 @@ export class IncludeProcessor {
353363
private async resolveLuaurcFileAliases(sourceFile:string, rcMap: LuauRCRequireMap) : Promise<RequireMap> {
354364
const map: RequireMap = {};
355365
let dir = normalizePath(sourceFile);
356-
while(true) {
366+
let last = dir;
367+
let limit = 25;
368+
while(limit-- > 0) {
357369
dir = normalizePath(path.dirname(dir));
358-
if(dir.length < 3) break;
370+
if(last == dir) {
371+
break;
372+
}
359373
const norm = normalizeJoinPath(dir,".luaurc");
360374
let dirMap = rcMap[norm] ?? null;
361375
if(!dirMap) {

src/shared/parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,8 @@ export class Parser {
17481748
[TokenType.PAREN_OPEN,"("],
17491749
[TokenType.PAREN_CLOSE,")"],
17501750
[TokenType.NEWLINE,"\n"],
1751-
[TokenType.LINE_COMMENT, `${languageConfig.lineCommentPrefix}@line 1 "${this.sourceFile}"`],
1751+
// [TokenType.LINE_COMMENT, `${languageConfig.lineCommentPrefix}@line 1 "${this.sourceFile}"`],
1752+
[TokenType.LINE_COMMENT, `${languageConfig.lineCommentPrefix}@line 1 "${this.formatPathForLineDirective(this.sourceFile)}"`],
17521753
[TokenType.NEWLINE,"\n"],
17531754
];
17541755

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ export class VSCodeHost implements HostInterface {
442442
return vscode.Uri.file(fileName).toString();
443443
}
444444

445+
const relatives = [];
446+
445447
// Find which workspace folder contains this file
446448
for (const folder of workspaceFolders) {
447449
const folderPath = folder.uri.fsPath;
@@ -452,8 +454,19 @@ export class VSCodeHost implements HostInterface {
452454
const normalizedRelative = relativePath.split(path.sep).join('/');
453455
// Include folder name in URI to identify which workspace root
454456
return `workspace:///${folder.name}/${normalizedRelative}`;
457+
} else {
458+
const relativePath = path.relative(folderPath, fileName);
459+
if(relativePath.startsWith('..')) {
460+
relatives.push(`workspace:///${folder.name}/${relativePath}`);
461+
}
455462
}
456463
}
464+
if(relatives.length > 0) {
465+
relatives.sort((a,b) => a.length - b.length);
466+
return relatives[0];
467+
}
468+
469+
// return `workspace:///` + vscode.workspace.asRelativePath(fileName);
457470

458471
// File is outside all workspace folders - return absolute file:// URL
459472
return vscode.Uri.file(fileName).toString();

0 commit comments

Comments
 (0)