Skip to content

Commit

Permalink
fix: wrong build file associated
Browse files Browse the repository at this point in the history
  • Loading branch information
llogick committed Dec 24, 2023
1 parent 5c0bebe commit b2740b2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/ComptimeInterpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ pub fn interpret(
} };
}

const import_uri = (try interpreter.document_store.uriFromImportStr(interpreter.allocator, interpreter.getHandle().*, import_str[1 .. import_str.len - 1])) orelse return error.ImportFailure;
const import_uri = (try interpreter.document_store.uriFromImportStr(interpreter.allocator, interpreter.getHandle(), import_str[1 .. import_str.len - 1])) orelse return error.ImportFailure;

Check warning on line 895 in src/ComptimeInterpreter.zig

View check run for this annotation

Codecov / codecov/patch

src/ComptimeInterpreter.zig#L895

Added line #L895 was not covered by tests
defer interpreter.allocator.free(import_uri);

const import_handle = interpreter.document_store.getOrLoadHandle(import_uri) orelse return error.ImportFailure;
Expand Down
299 changes: 172 additions & 127 deletions src/DocumentStore.zig

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,20 @@ pub const Status = enum {
const Job = union(enum) {
incoming_message: std.json.Parsed(Message),
generate_diagnostics: DocumentStore.Uri,
load_build_configuration: DocumentStore.Uri,
load_build_configuration: struct {
build_file_uri: DocumentStore.Uri,
maybe_handle_uri: ?DocumentStore.Uri = null,
},
run_build_on_save,

fn deinit(self: Job, allocator: std.mem.Allocator) void {
switch (self) {
.incoming_message => |parsed_message| parsed_message.deinit(),
.generate_diagnostics => |uri| allocator.free(uri),
.load_build_configuration => |uri| allocator.free(uri),
.load_build_configuration => |params| {
allocator.free(params.build_file_uri);
if (params.maybe_handle_uri) |handle_uri| allocator.free(handle_uri);

Check warning on line 157 in src/Server.zig

View check run for this annotation

Codecov / codecov/patch

src/Server.zig#L156-L157

Added lines #L156 - L157 were not covered by tests
},
.run_build_on_save => {},
}
}
Expand Down Expand Up @@ -681,7 +687,7 @@ fn invalidateAllBuildFiles(server: *Server) error{OutOfMemory}!void {
try server.job_queue.ensureUnusedCapacity(server.document_store.build_files.count());
for (server.document_store.build_files.keys()) |build_file_uri| {
server.job_queue.writeItemAssumeCapacity(.{
.load_build_configuration = try server.allocator.dupe(u8, build_file_uri),
.load_build_configuration = .{ .build_file_uri = try server.allocator.dupe(u8, build_file_uri) },

Check warning on line 690 in src/Server.zig

View check run for this annotation

Codecov / codecov/patch

src/Server.zig#L690

Added line #L690 was not covered by tests
});
}
}
Expand Down Expand Up @@ -1195,7 +1201,7 @@ fn saveDocumentHandler(server: *Server, arena: std.mem.Allocator, notification:

if (std.process.can_spawn and DocumentStore.isBuildFile(uri)) {
try server.pushJob(.{
.load_build_configuration = try server.allocator.dupe(u8, uri),
.load_build_configuration = .{ .build_file_uri = try server.allocator.dupe(u8, uri) },
});
}

Expand Down Expand Up @@ -2031,10 +2037,10 @@ fn processJob(server: *Server, job: Job, wait_group: ?*std.Thread.WaitGroup) voi
const json_message = server.sendToClientNotification("textDocument/publishDiagnostics", diagnostics) catch return;
server.allocator.free(json_message);
},
.load_build_configuration => |build_file_uri| {
.load_build_configuration => |params| {
std.debug.assert(std.process.can_spawn);
if (!std.process.can_spawn) return;
server.document_store.invalidateBuildFile(build_file_uri) catch return;
server.document_store.invalidateBuildFile(params.build_file_uri, params.maybe_handle_uri) catch return;
},
.run_build_on_save => {
std.debug.assert(std.process.can_spawn);
Expand Down
8 changes: 4 additions & 4 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1507,11 +1507,11 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
const import_str = tree.tokenSlice(main_tokens[import_param]);
const import_uri = (try analyser.store.uriFromImportStr(
analyser.arena.allocator(),
handle.*,
handle,
import_str[1 .. import_str.len - 1],
)) orelse (try analyser.store.uriFromImportStr(
analyser.arena.allocator(),
if (analyser.root_handle) |root_handle| root_handle.* else return null,
if (analyser.root_handle) |root_handle| root_handle else return null,

Check warning on line 1514 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L1514

Added line #L1514 was not covered by tests
import_str[1 .. import_str.len - 1],
)) orelse return null;

Expand All @@ -1520,7 +1520,7 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
// reference to node '0' which is root
return TypeWithHandle.typeVal(.{ .node = 0, .handle = new_handle });
} else if (std.mem.eql(u8, call_name, "@cImport")) {
const cimport_uri = (try analyser.store.resolveCImport(handle.*, node)) orelse return null;
const cimport_uri = (try analyser.store.resolveCImport(handle, node)) orelse return null;

Check warning on line 1523 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L1523

Added line #L1523 was not covered by tests

const new_handle = analyser.store.getOrLoadHandle(cimport_uri) orelse return null;

Expand Down Expand Up @@ -2533,7 +2533,7 @@ pub fn getFieldAccessType(
.start = import_str_tok.loc.start + 1,
.end = import_str_tok.loc.end - 1,
});
const uri = try analyser.store.uriFromImportStr(analyser.arena.allocator(), curr_handle.*, import_str) orelse return null;
const uri = try analyser.store.uriFromImportStr(analyser.arena.allocator(), curr_handle, import_str) orelse return null;

Check warning on line 2536 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L2536

Added line #L2536 was not covered by tests
const node_handle = analyser.store.getOrLoadHandle(uri) orelse return null;
current_type = TypeWithHandle.typeVal(NodeWithHandle{ .handle = node_handle, .node = 0 });
_ = tokenizer.next(); // eat the .r_paren
Expand Down
19 changes: 10 additions & 9 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ fn completeError(server: *Server, arena: std.mem.Allocator, handle: *DocumentSto
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

return try server.document_store.errorCompletionItems(arena, handle.*);
return try server.document_store.errorCompletionItems(arena, handle);
}

fn kindToSortScore(kind: types.CompletionItemKind) ?[]const u8 {
Expand Down Expand Up @@ -808,7 +808,7 @@ fn completeDot(document_store: *DocumentStore, analyser: *Analyser, arena: std.m
if (token_tags[dot_token_index - 1] == .number_literal or token_tags[dot_token_index - 1] != .equal) return &.{};

// `var enum_val = .` or the get*Context logic failed because of syntax errors (parser didn't create the necessary node(s))
const enum_completions = try document_store.enumCompletionItems(arena, handle.*);
const enum_completions = try document_store.enumCompletionItems(arena, handle);

Check warning on line 811 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L811

Added line #L811 was not covered by tests
return enum_completions;
}

Expand All @@ -820,7 +820,7 @@ fn completeDot(document_store: *DocumentStore, analyser: *Analyser, arena: std.m
fn completeFileSystemStringLiteral(
arena: std.mem.Allocator,
store: *DocumentStore,
handle: DocumentStore.Handle,
handle: *DocumentStore.Handle,
pos_context: Analyser.PositionContext,
) ![]types.CompletionItem {
var completions: DocumentScope.CompletionSet = .{};
Expand Down Expand Up @@ -893,10 +893,11 @@ fn completeFileSystemStringLiteral(
}

if (completing.len == 0 and pos_context == .import_string_literal) {
if (handle.associated_build_file) |uri| blk: {
if (handle.getAssociatedBuildFileUri(store, true)) |uri| blk: {
defer handle.unlockShared();

Check warning on line 897 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L896-L897

Added lines #L896 - L897 were not covered by tests
const build_file = store.getBuildFile(uri).?;
const build_config = build_file.tryLockConfig() orelse break :blk;
defer build_file.unlockConfig();
const build_config = build_file.getConfig() orelse break :blk;
defer build_file.unlockShared();

Check warning on line 900 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L899-L900

Added lines #L899 - L900 were not covered by tests

try completions.ensureUnusedCapacity(arena, build_config.packages.len);
for (build_config.packages) |pkg| {
Expand All @@ -908,8 +909,8 @@ fn completeFileSystemStringLiteral(
}
} else if (DocumentStore.isBuildFile(handle.uri)) blk: {
const build_file = store.getBuildFile(handle.uri) orelse break :blk;
const build_config = build_file.tryLockConfig() orelse break :blk;
defer build_file.unlockConfig();
const build_config = build_file.getConfig() orelse break :blk;
defer build_file.unlockShared();

Check warning on line 913 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L912-L913

Added lines #L912 - L913 were not covered by tests

try completions.ensureUnusedCapacity(arena, build_config.deps_build_roots.len);
for (build_config.deps_build_roots) |dbr| {
Expand Down Expand Up @@ -970,7 +971,7 @@ pub fn completionAtIndex(server: *Server, analyser: *Analyser, arena: std.mem.Al
.string_literal,
=> blk: {
if (pos_context == .string_literal and !DocumentStore.isBuildFile(handle.uri)) break :blk null;
break :blk completeFileSystemStringLiteral(arena, &server.document_store, handle.*, pos_context) catch |err| {
break :blk completeFileSystemStringLiteral(arena, &server.document_store, handle, pos_context) catch |err| {

Check warning on line 974 in src/features/completions.zig

View check run for this annotation

Codecov / codecov/patch

src/features/completions.zig#L974

Added line #L974 was not covered by tests
log.err("failed to get file system completions: {}", .{err});
return null;
};
Expand Down
4 changes: 2 additions & 2 deletions src/features/goto.zig
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ fn gotoDefinitionString(
const uri = switch (pos_context) {
.import_string_literal,
.embedfile_string_literal,
=> try document_store.uriFromImportStr(arena, handle.*, import_str),
=> try document_store.uriFromImportStr(arena, handle, import_str),

Check warning on line 207 in src/features/goto.zig

View check run for this annotation

Codecov / codecov/patch

src/features/goto.zig#L207

Added line #L207 was not covered by tests
.cinclude_string_literal => try URI.fromPath(
arena,
blk: {
if (std.fs.path.isAbsolute(import_str)) break :blk import_str;
var include_dirs: std.ArrayListUnmanaged([]const u8) = .{};
_ = document_store.collectIncludeDirs(arena, handle.*, &include_dirs) catch |err| {
_ = document_store.collectIncludeDirs(arena, handle, &include_dirs) catch |err| {

Check warning on line 213 in src/features/goto.zig

View check run for this annotation

Codecov / codecov/patch

src/features/goto.zig#L213

Added line #L213 was not covered by tests
log.err("failed to resolve include paths: {}", .{err});
return null;
};
Expand Down
2 changes: 1 addition & 1 deletion src/features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn gatherReferences(

var handle_dependencies = std.ArrayListUnmanaged([]const u8){};
defer handle_dependencies.deinit(allocator);
try analyser.store.collectDependencies(allocator, handle.*, &handle_dependencies);
try analyser.store.collectDependencies(allocator, handle, &handle_dependencies);

try dependencies.ensureUnusedCapacity(allocator, handle_dependencies.items.len);
for (handle_dependencies.items) |uri| {
Expand Down

0 comments on commit b2740b2

Please sign in to comment.