Skip to content

Fix the suffix bug in Windows#16

Open
Qxy0happy wants to merge 1 commit into
soypat:mainfrom
Qxy0happy:fix-suffix-bug-windows
Open

Fix the suffix bug in Windows#16
Qxy0happy wants to merge 1 commit into
soypat:mainfrom
Qxy0happy:fix-suffix-bug-windows

Conversation

@Qxy0happy
Copy link
Copy Markdown

This PR fixes the suffix bug on Windows.

Copilot AI review requested due to automatic review settings March 30, 2026 01:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix a Windows-specific executable suffix issue when compiling/running exercises, and also updates the module’s Go/tooling dependencies.

Changes:

  • Update exercise compilation to return a temp executable path (instead of an open file handle) and attempt to append .exe on Windows.
  • Add additional error checks around CLI output printing.
  • Bump go.mod Go version and upgrade several dependencies (with corresponding go.sum updates).

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 4 comments.

File Description
cmd/gopherlings/main.go Adjusts temp executable creation/execution flow and adds Windows-specific naming logic.
go.mod Updates Go version directive and bumps direct/indirect dependencies.
go.sum Updates checksums to match the dependency upgrades.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/gopherlings/main.go
Comment on lines +55 to +58
_, err = red.Fprintln(os.Stderr, err)
if err != nil {
os.Exit(1)
}
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

main no longer exits with a non-zero status when rootCmd.Execute() returns an error. After printing the error, the program falls through and returns exit code 0 unless printing fails, which will mask failures for callers/CI. Ensure the error path still calls os.Exit(1) (or returns an error to main).

Suggested change
_, err = red.Fprintln(os.Stderr, err)
if err != nil {
os.Exit(1)
}
_, printErr := red.Fprintln(os.Stderr, err)
if printErr != nil {
os.Exit(1)
}
os.Exit(1)

Copilot uses AI. Check for mistakes.
Comment thread cmd/gopherlings/main.go
Comment on lines +379 to 382
if runtime.GOOS == "windows" {
name += ".exe"
}
f, err := os.CreateTemp("", name)
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

On Windows, appending .exe to the pattern passed to os.CreateTemp doesn't guarantee the resulting filename ends with .exe (CreateTemp appends random characters after the pattern unless you include *). This can still produce a non-executable path like foo.exe12345, so the Windows suffix issue likely persists. Use a pattern like name+"*.exe" (or create a temp dir and join name+".exe") so the final file actually has the .exe suffix.

Suggested change
if runtime.GOOS == "windows" {
name += ".exe"
}
f, err := os.CreateTemp("", name)
pattern := name
if runtime.GOOS == "windows" {
pattern = name + "*.exe"
}
f, err := os.CreateTemp("", pattern)

Copilot uses AI. Check for mistakes.
Comment thread cmd/gopherlings/main.go
return "", err
}
tmpPath := f.Name()
f.Close()
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

The result of f.Close() is ignored. Since the file must be closed on Windows before it can be executed/overwritten reliably, handle the close error and clean up the temp file on failure.

Suggested change
f.Close()
if err := f.Close(); err != nil {
// Best-effort cleanup if we fail to close the temp file.
_ = os.Remove(tmpPath)
return "", err
}

Copilot uses AI. Check for mistakes.
Comment thread go.mod
Comment on lines +3 to +8
go 1.26

require (
github.com/fatih/color v1.13.0
github.com/fsnotify/fsnotify v1.5.4
github.com/spf13/cobra v1.5.0
github.com/fatih/color v1.19.0
github.com/fsnotify/fsnotify v1.9.0
github.com/spf13/cobra v1.10.2
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

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

The PR description is about a Windows suffix bug, but this change also bumps the module go version from 1.18 to 1.26 and updates multiple dependencies. Raising the go directive is a compatibility-breaking change for users/build environments pinned to older Go versions, so it should be justified in the PR (or reverted if not required for the fix).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@copilot apply changes based on this feedback

@soypat
Copy link
Copy Markdown
Owner

soypat commented Mar 30, 2026

What is the windows suffix bug? @Qxy0happy

@Qxy0happy
Copy link
Copy Markdown
Author

During the use of this tool, the following error was encountered:
gopherlings on git main via go v1.26.1 took 1m0s

.\cmd\gopherlings\gopherlings.exe watch
Compiling .\exercises\001-hello
Executing .\exercises\001-hello

exec: "D:\Cache\Temp\User\30625\001-hello633839467": executable file not found in %PATH%

Subsequent analysis indicates that this issue arises from the suffix automatically appended by the Windows operating system. Consequently, the program is unable to locate the file corresponding to f.Name().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants