Skip to content
Merged
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
2 changes: 1 addition & 1 deletion compiled_starters/zig/.codecrafters/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

set -e # Exit on failure

exec $(dirname $0)/zig-out/bin/main "$@"
exec "$(dirname "$0")"/zig-out/bin/main "$@"
2 changes: 1 addition & 1 deletion compiled_starters/zig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Time to move on to the next stage!

Note: This section is for stages 2 and beyond.

1. Ensure you have `zig (0.14)` installed locally
1. Ensure you have `zig (0.15)` installed locally
1. Run `./your_program.sh` to run your program, which is implemented in
`src/main.zig`.
1. Commit your changes and run `git push origin master` to submit your solution
Expand Down
7 changes: 4 additions & 3 deletions compiled_starters/zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "main",
.root_source_file = b.path("src/main.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host,
}),
Copy link

Choose a reason for hiding this comment

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

Bug: Build Configuration Locks Target and Optimization

The build.zig configuration hardcodes the build target to b.graph.host and removes the optimization option. This change eliminates the ability to cross-compile or specify optimization levels via zig build command-line arguments, reducing build flexibility.

Additional Locations (1)

Fix in Cursor Fix in Web

});

// This declares intent for the executable to be installed into the
Expand Down
2 changes: 1 addition & 1 deletion compiled_starters/zig/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
Expand Down
4 changes: 2 additions & 2 deletions compiled_starters/zig/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ debug: false
# Use this to change the Zig version used to run your code
# on Codecrafters.
#
# Available versions: zig-0.14
buildpack: zig-0.14
# Available versions: zig-0.15
buildpack: zig-0.15
10 changes: 8 additions & 2 deletions compiled_starters/zig/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ pub fn main() !void {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;

if (args.len < 3) {
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.flush();
return;
}

Expand All @@ -28,6 +33,7 @@ pub fn main() !void {
// _ = try file.seekTo(16);
// _ = try file.read(&buf);
// const page_size = std.mem.readInt(u16, &buf, .big);
// try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
// try stdout.print("database page size: {}\n", .{page_size});
// try stdout.flush();
}
}
2 changes: 1 addition & 1 deletion compiled_starters/zig/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec $(dirname $0)/zig-out/bin/main "$@"
exec "$(dirname "$0")"/zig-out/bin/main "$@"
30 changes: 30 additions & 0 deletions dockerfiles/zig-0.15.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# syntax=docker/dockerfile:1.7-labs
FROM debian:bookworm

RUN apt-get update && \
apt-get install --no-install-recommends -y xz-utils=5.4.1-1 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Download and install Zig
RUN curl -O https://ziglang.org/download/0.15.1/zig-x86_64-linux-0.15.1.tar.xz \
&& tar -xf zig-x86_64-linux-0.15.1.tar.xz \
&& mv zig-x86_64-linux-0.15.1 /usr/local/zig \
&& rm zig-x86_64-linux-0.15.1.tar.xz

# Add Zig to PATH
ENV PATH="/usr/local/zig:${PATH}"

ENV CODECRAFTERS_DEPENDENCY_FILE_PATHS="build.zig,build.zig.zon"

WORKDIR /app

# .git & README.md are unique per-repository. We ignore them on first copy to prevent cache misses
COPY --exclude=.git --exclude=README.md . /app

# This runs zig build
RUN .codecrafters/compile.sh

# Cache build directory
RUN mkdir -p /app-cached
RUN mv /app/.zig-cache /app-cached/.zig-cache || true
2 changes: 1 addition & 1 deletion solutions/zig/01-dr6/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

set -e # Exit on failure

exec $(dirname $0)/zig-out/bin/main "$@"
exec "$(dirname "$0")"/zig-out/bin/main "$@"
2 changes: 1 addition & 1 deletion solutions/zig/01-dr6/code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Time to move on to the next stage!

Note: This section is for stages 2 and beyond.

1. Ensure you have `zig (0.14)` installed locally
1. Ensure you have `zig (0.15)` installed locally
1. Run `./your_program.sh` to run your program, which is implemented in
`src/main.zig`.
1. Commit your changes and run `git push origin master` to submit your solution
Expand Down
7 changes: 4 additions & 3 deletions solutions/zig/01-dr6/code/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "main",
.root_source_file = b.path("src/main.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host,
}),
});

// This declares intent for the executable to be installed into the
Expand Down
2 changes: 1 addition & 1 deletion solutions/zig/01-dr6/code/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
Expand Down
4 changes: 2 additions & 2 deletions solutions/zig/01-dr6/code/codecrafters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ debug: false
# Use this to change the Zig version used to run your code
# on Codecrafters.
#
# Available versions: zig-0.14
buildpack: zig-0.14
# Available versions: zig-0.15
buildpack: zig-0.15
10 changes: 8 additions & 2 deletions solutions/zig/01-dr6/code/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ pub fn main() !void {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;

if (args.len < 3) {
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.flush();
Copy link

Choose a reason for hiding this comment

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

Bug: Invalid API Usage Causes Compilation Errors

The stdout_writer initialization uses an invalid Zig API (std.fs.File.stdout().writer(&buffer).interface), which will cause compilation errors. This change also redirects usage error messages from stderr to stdout, which goes against standard Unix conventions.

Additional Locations (2)

Fix in Cursor Fix in Web

return;
}

Expand All @@ -24,6 +29,7 @@ pub fn main() !void {
_ = try file.seekTo(16);
_ = try file.read(&buf);
const page_size = std.mem.readInt(u16, &buf, .big);
try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
try stdout.print("database page size: {}\n", .{page_size});
try stdout.flush();
}
}
2 changes: 1 addition & 1 deletion solutions/zig/01-dr6/code/your_program.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ set -e # Exit early if any commands fail
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec $(dirname $0)/zig-out/bin/main "$@"
exec "$(dirname "$0")"/zig-out/bin/main "$@"
17 changes: 11 additions & 6 deletions solutions/zig/01-dr6/diff/src/main.zig.diff
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
@@ -1,33 +1,29 @@
const std = @import("std");

@@ -3,37 +3,33 @@
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
Expand All @@ -9,8 +7,13 @@
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;

if (args.len < 3) {
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.flush();
return;
}

Expand All @@ -29,11 +32,13 @@
- // _ = try file.seekTo(16);
- // _ = try file.read(&buf);
- // const page_size = std.mem.readInt(u16, &buf, .big);
- // try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
- // try stdout.print("database page size: {}\n", .{page_size});
- // try stdout.flush();
+ var buf: [2]u8 = undefined;
+ _ = try file.seekTo(16);
+ _ = try file.read(&buf);
+ const page_size = std.mem.readInt(u16, &buf, .big);
+ try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
+ try stdout.print("database page size: {}\n", .{page_size});
+ try stdout.flush();
}
}
3 changes: 2 additions & 1 deletion solutions/zig/01-dr6/explanation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var buf: [2]u8 = undefined;
_ = try file.seekTo(16);
_ = try file.read(&buf);
const page_size = std.mem.readInt(u16, &buf, .big);
try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
try stdout.print("database page size: {}\n", .{page_size});
try stdout.flush();
```

Push your changes to pass the first stage:
Expand Down
2 changes: 1 addition & 1 deletion starter_templates/zig/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

set -e # Exit on failure

exec $(dirname $0)/zig-out/bin/main "$@"
exec "$(dirname "$0")"/zig-out/bin/main "$@"
7 changes: 4 additions & 3 deletions starter_templates/zig/code/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const exe = b.addExecutable(.{
.name = "main",
.root_source_file = b.path("src/main.zig"),
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = b.graph.host,
}),
});

// This declares intent for the executable to be installed into the
Expand Down
2 changes: 1 addition & 1 deletion starter_templates/zig/code/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
.minimum_zig_version = "0.14.0",
.minimum_zig_version = "0.15.1",

// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.
Expand Down
10 changes: 8 additions & 2 deletions starter_templates/zig/code/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ pub fn main() !void {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;

if (args.len < 3) {
try std.io.getStdErr().writer().print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.print("Usage: {s} <database_file_path> <command>\n", .{args[0]});
try stdout.flush();
Copy link

Choose a reason for hiding this comment

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

Bug: Unix Conventions Violated: Error Messages Redirected

Usage error messages are now printed to stdout instead of stderr. This change breaks standard Unix conventions for command-line tools, affecting script handling and output redirection.

Additional Locations (1)

Fix in Cursor Fix in Web

return;
}

Expand All @@ -28,6 +33,7 @@ pub fn main() !void {
// _ = try file.seekTo(16);
// _ = try file.read(&buf);
// const page_size = std.mem.readInt(u16, &buf, .big);
// try std.io.getStdOut().writer().print("database page size: {}\n", .{page_size});
// try stdout.print("database page size: {}\n", .{page_size});
// try stdout.flush();
}
}
2 changes: 1 addition & 1 deletion starter_templates/zig/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
attributes:
required_executable: zig (0.14)
required_executable: zig (0.15)
user_editable_file: src/main.zig
Loading