Skip to content
Open
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
Binary file modified server/resources/compiler/linux/nwn_script_comp
Binary file not shown.
Binary file modified server/resources/compiler/mac/nwn_script_comp
Binary file not shown.
Binary file modified server/resources/compiler/windows/nwn_script_comp.exe
Binary file not shown.
60 changes: 42 additions & 18 deletions server/src/Providers/DiagnosticsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,27 @@ export default class DiagnoticsProvider extends Provider {

private generateDiagnostics(uris: string[], files: FilesDiagnostics, severity: DiagnosticSeverity) {
return (line: string) => {
const uri = uris.find((uri) => basename(fileURLToPath(uri)) === lineFilename.exec(line)![2]);

if (uri) {
const linePosition = Number(lineNumber.exec(line)![1]) - 1;
const diagnostic = {
severity,
range: {
start: { line: linePosition, character: 0 },
end: { line: linePosition, character: Number.MAX_VALUE },
},
message: lineMessage.exec(line)![2].trim(),
};

files[uri].push(diagnostic);
const lineFilenameMatch = lineFilename.exec(line);
if (lineFilenameMatch) {
const reportedFileName = lineFilenameMatch[2];
const uri = uris.find((uri) => basename(fileURLToPath(uri)) === reportedFileName);
if (uri) {
const lineNumberMatch = lineNumber.exec(line);
const lineMessageMatch = lineMessage.exec(line);
if (lineNumberMatch && lineMessageMatch) {
const linePosition = Number(lineNumberMatch[1]) - 1;
const diagnostic = {
severity,
range: {
start: { line: linePosition, character: 0 },
end: { line: linePosition, character: Number.MAX_VALUE },
},
message: lineMessageMatch[2].trim(),
};

files[uri].push(diagnostic);
}
}
}
};
}
Expand Down Expand Up @@ -101,7 +108,10 @@ export default class DiagnoticsProvider extends Provider {
// The compiler command:
// - y; continue on error
// - s; dry run
const args = ["-y", "-s"];
// - n; no entry point required (for include files)
// - E; collect and report all errors (not just the first)
// Note: -E requires compiler binary with ABI v2+
const args = ["-y", "-s", "-n", "-E"];
if (Boolean(nwnHome)) {
args.push("--userdirectory");
args.push(`"${nwnHome}"`);
Expand All @@ -114,12 +124,25 @@ export default class DiagnoticsProvider extends Provider {
} else if (verbose) {
this.server.logger.info("Trying to resolve Neverwinter Nights installation directory automatically.");
}
if (children.length > 0) {
// Collect directories from ALL indexed documents so the compiler can
// resolve the full transitive include chain, not just direct children.
const allDirs: Set<string> = new Set();
this.server.documentsCollection.forEach((doc) => {
if (doc.uri && doc.uri.startsWith("file://")) {
allDirs.add(dirname(fileURLToPath(doc.uri)));
}
});
Comment on lines +127 to +134
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this doesn't cause performance issues?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's obviously less performant, but the hit is moderate.
Even with hundreds of scripts, even >1k, we are talking about an O(n) iteration over them on each file save and each queued document. In practice so far, for me, it's not been noticeable. The start-up indexing period takes the biggest hit but it was already slow and the difference is not that big.

If we wanted to optimize this, I could do some caching on ServerManager and invalidate it only when documents are added/removed so we don't have to recompute on every publish call.

However, there is a potential performance hit here that's bigger, and that's the amount of directories we pass to "--dirs", it might affect compilation time if the dirs list is very large. I had to do this to get the new compiler to work, but there might be a better way. I'll have a think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try and see if I can walk the tree of children to minimize it and not mess up compilation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, we can't as I remembered.

We can't narrow this to just transitive children from getChildren() because the tokenizer's include tracking may be incomplete compared to what the compiler actually resolves (e.g. deep/nested transitive includes, conditional includes, or files the tokenizer hasn't fully parsed).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try it this way then, thank you for validating.

// Also add the compiled file's own directory
allDirs.add(dirname(fileURLToPath(uri)));
if (allDirs.size > 0) {
args.push("--dirs");
args.push(`"${[...new Set(uris.map((uri) => dirname(fileURLToPath(uri))))].join(",")}"`);
args.push(`"${[...allDirs].join(",")}"`);
}

const filePath = fileURLToPath(uri);
this.server.logger.info(`Compiling file: ${filePath}`);
args.push("-c");
args.push(`"${fileURLToPath(uri)}"`);
args.push(`"${filePath}"`);

let stdout = "";
let stderr = "";
Expand Down Expand Up @@ -186,6 +209,7 @@ export default class DiagnoticsProvider extends Provider {
for (const [uri, diagnostics] of Object.entries(files)) {
this.server.connection.sendDiagnostics({ uri, diagnostics });
}

resolve(true);
});
});
Expand Down