Skip to content
Closed
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
39 changes: 34 additions & 5 deletions cmd/internal/migrations/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,40 @@ func MigrateTrustedProxyConfig(cmd *cobra.Command, cwd string, _, _ *semver.Vers
// listener configuration fields.
func MigrateConfigListenerFields(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
err := internal.ChangeFileContent(cwd, func(content string) string {
replacer := strings.NewReplacer(
"Prefork:", "EnablePrefork:",
"Network:", "ListenerNetwork:",
)
return replacer.Replace(content)
// collect fields that were moved from Config to ListenConfig
moved := []string{}
reMoved := regexp.MustCompile(`(?m)^\s*(Prefork|Network|DisableStartupMessage|EnablePrintRoutes):\s*([^,]+),?\n`)
content = reMoved.ReplaceAllStringFunc(content, func(m string) string {
sub := reMoved.FindStringSubmatch(m)
if len(sub) == 3 {
name := sub[1]
val := strings.TrimSpace(sub[2])
switch name {
case "Prefork":
name = "EnablePrefork"
case "Network":
name = "ListenerNetwork"
}
moved = append(moved, fmt.Sprintf("%s: %s", name, val))
}
return ""
})

if len(moved) == 0 {
return content
}

// inject ListenConfig with the moved fields if Listen is used without config
reListen := regexp.MustCompile(`\.Listen\(([^,\n]+)\)`)
content = reListen.ReplaceAllStringFunc(content, func(m string) string {
if strings.Contains(m, "ListenConfig{") {
return m
}
addr := reListen.FindStringSubmatch(m)[1]
return fmt.Sprintf(".Listen(%s, fiber.ListenConfig{%s})", addr, strings.Join(moved, ", "))
})

return content
Comment on lines +391 to +401
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The logic for injecting the moved listener configuration fields is incomplete. It only handles app.Listen calls with a single argument and does not modify calls that already include a fiber.ListenConfig. This can lead to an incorrect migration where configuration is lost if a ListenConfig is already present.

To make the migration more robust, it should handle both cases: adding a new ListenConfig and injecting fields into an existing one.

		// inject ListenConfig with the moved fields
		newFieldsStr := strings.Join(moved, ", ")

		// First, try to inject into an existing ListenConfig.
		reListenWithCfg := regexp.MustCompile(`(\.Listen\([^,]+,\s*fiber\.ListenConfig{)([^}]*)(\})`)
		content = reListenWithCfg.ReplaceAllStringFunc(content, func(m string) string {
			sub := reListenWithCfg.FindStringSubmatch(m)
			prefix, existing, suffix := sub[1], strings.TrimSpace(sub[2]), sub[3]
			if existing != "" && !strings.HasSuffix(existing, ",") {
				existing += ","
			}
			return fmt.Sprintf("%s%s %s%s", prefix, existing, newFieldsStr, suffix)
		})

		// Then, handle cases without an existing ListenConfig.
		reListenSimple := regexp.MustCompile(`(\.Listen\()([^,)]+)(\))`) // It specifically looks for Listen calls with only one argument.
		content = reListenSimple.ReplaceAllString(content, fmt.Sprintf(`${1}${2}, fiber.ListenConfig{%s}${3}`, newFieldsStr))

		return content

})
if err != nil {
return fmt.Errorf("failed to migrate listener related config fields: %w", err)
Expand Down
7 changes: 4 additions & 3 deletions cmd/internal/migrations/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,17 +485,18 @@ func main() {
app := fiber.New(fiber.Config{
Prefork: true,
Network: "tcp",
DisableStartupMessage: true,
EnablePrintRoutes: true,
})
_ = app
app.Listen(":3000")
}`)

var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateConfigListenerFields(cmd, dir, nil, nil))

content := readFile(t, file)
assert.Contains(t, content, "EnablePrefork: true")
assert.Contains(t, content, "ListenerNetwork: \"tcp\"")
assert.Contains(t, content, "fiber.ListenConfig{EnablePrefork: true, ListenerNetwork: \"tcp\", DisableStartupMessage: true, EnablePrintRoutes: true}")
assert.Contains(t, buf.String(), "Migrating listener related config fields")
}
Comment on lines 496 to 501
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The test coverage for MigrateConfigListenerFields could be improved. The current test only covers the scenario where app.Listen is called with a single argument. To ensure the migration is robust, consider adding test cases for other scenarios, such as:

  • app.Listen is called with an existing, empty fiber.ListenConfig{}.
  • app.Listen is called with an existing fiber.ListenConfig containing other options.
  • No app.Listen call is present in the code, to verify how the moved fields are handled (or if configuration loss is an acceptable trade-off).

Adding these tests would help prevent regressions and ensure the migration handles more edge cases correctly.


Expand Down
Loading