Skip to content

Conversation

@jamietanna
Copy link
Member

@jamietanna jamietanna commented May 5, 2025

Although this is less necessary after bumping our minimum Go version to
1.22 (in 0e8156a), it's still
worthwhile ensuring that the module we're generating the code for is
correctly set to Go 1.22, otherwise users will receive:

HTTP/1.1 404 Not Found
...
404 page not found

To do this, we can introduce a new means to flag "warnings" that need to
be output.

We need to make sure we're using ParseLax to reduce the risk of new
directives or content being introduced into a go.mod breaking
oapi-codegen.

This is a step towards being able to implement future warnings to ensure
that consumers are using correct Go versions.

Closes #1628.


Warning

I'm not quite sure about this - especially for oapi-codegen-as-a-library

@jamietanna jamietanna force-pushed the warn/gomod branch 2 times, most recently from 1adcd35 to bfcdd6f Compare May 5, 2025 14:52
@jamietanna jamietanna requested a review from Copilot May 9, 2025 06:24
Copy link

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 introduces a mechanism to warn users when the target module's Go version is below 1.22 for the std-http-server generation, helping avoid runtime issues such as unexpected 404 responses. Key changes include:

  • Adding functions to locate and parse go.mod/tools.mod files and verify the Go version.
  • Implementing warning generation in GenerateOptions and integrating warning output in the CLI.
  • Updating the main command to display warnings when present.

Reviewed Changes

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

File Description
pkg/codegen/minimum_go_version.go Introduces functions to search for and parse go.mod/tools.mod and checks for minimum Go version.
pkg/codegen/configuration.go Adds warnings functionality for std-http-server generation based on the Go version in the module file.
cmd/oapi-codegen/oapi-codegen.go Integrates warning output by printing any warnings returned from GenerateOptions into stderr.
Files not reviewed (1)
  • go.mod: Language not supported

}

parentDir := filepath.Dir(currentDir)
if parentDir == "/" {
Copy link

Copilot AI May 9, 2025

Choose a reason for hiding this comment

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

On non-Unix systems, using a hard-coded "/" as the root directory may lead to improper behavior; consider using a cross-platform approach to determine when to stop recursing upward.

Suggested change
if parentDir == "/" {
if parentDir == currentDir {

Copilot uses AI. Check for mistakes.
@jamietanna jamietanna marked this pull request as ready for review May 9, 2025 07:23
@jamietanna jamietanna requested a review from a team as a code owner May 9, 2025 07:23
Although this is less necessary after bumping our minimum Go version to
1.22 (in 0e8156a), it's still
worthwhile ensuring that the module we're generating the code for is
correctly set to Go 1.22, otherwise users will receive:

    HTTP/1.1 404 Not Found
    ...
    404 page not found

To do this, we can introduce a new means to flag "warnings" that need to
be output.

We need to make sure we're using `ParseLax` to reduce the risk of new
directives or content being introduced into a `go.mod` breaking
`oapi-codegen`.

This is a step towards being able to implement future warnings to ensure
that consumers are using correct Go versions.

Closes #1628.

[0]: https://www.jvt.me/posts/2024/03/04/go-net-http-why-404/
@jamietanna jamietanna merged commit 4d4ade2 into main May 9, 2025
35 checks passed
@jamietanna jamietanna deleted the warn/gomod branch May 9, 2025 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: warn when using std-http-server but /not/ using Go 1.22

2 participants