[GH-ISSUE #51] Limiting hosts per line works for first line, but after that each item goes on its own line #16

Closed
opened 2026-03-02 05:08:09 +03:00 by kerem · 10 comments
Owner

Originally created by @rfay on GitHub (Oct 28, 2023).
Original GitHub issue: https://github.com/goodhosts/hostsfile/issues/51

This isn't a huge deal, but probably not what's intended.

Using the artifacts in https://github.com/ddev/ddev/pull/4805 and running the manual test there,

for item in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do 
  sudo ddev hostname a$item.ddev.site 127.0.0.1
done

You'll see the results are that the first hosts line goes to 8, but after that they are all on separate lines.

127.0.0.1 localhost junk.ddev.site a1.ddev.site a2.ddev.site a3.ddev.site a4.ddev.site a5.ddev.site a6.ddev.site
127.0.0.1 a15.ddev.site
127.0.0.1 a14.ddev.site
127.0.0.1 a13.ddev.site
127.0.0.1 a12.ddev.site
127.0.0.1 a11.ddev.site
127.0.0.1 a10.ddev.site
127.0.0.1 a9.ddev.site
127.0.0.1 a8.ddev.site
127.0.0.1 a7.ddev.site

Code from github.com/rfay/ddev@b71df9536b/pkg/ddevapp/hostname_mgt.go (L135-L146) is

	hosts, err := goodhosts.NewCustomHosts(osHostsFilePath)

	if err != nil {
		return err
	}
	err = hosts.Add(ip, name)
	if err != nil {
		return err
	}
	hosts.HostsPerLine(8)
	err = hosts.Flush()
	return err
Originally created by @rfay on GitHub (Oct 28, 2023). Original GitHub issue: https://github.com/goodhosts/hostsfile/issues/51 This isn't a huge deal, but probably not what's intended. Using the artifacts in https://github.com/ddev/ddev/pull/4805 and running the manual test there, ``` for item in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do sudo ddev hostname a$item.ddev.site 127.0.0.1 done ``` You'll see the results are that the first hosts line goes to 8, but after that they are all on separate lines. ``` 127.0.0.1 localhost junk.ddev.site a1.ddev.site a2.ddev.site a3.ddev.site a4.ddev.site a5.ddev.site a6.ddev.site 127.0.0.1 a15.ddev.site 127.0.0.1 a14.ddev.site 127.0.0.1 a13.ddev.site 127.0.0.1 a12.ddev.site 127.0.0.1 a11.ddev.site 127.0.0.1 a10.ddev.site 127.0.0.1 a9.ddev.site 127.0.0.1 a8.ddev.site 127.0.0.1 a7.ddev.site ``` Code from https://github.com/rfay/ddev/blob/b71df9536b015854d9ee3cc7829861ef34e0e909/pkg/ddevapp/hostname_mgt.go#L135-L146 is ``` hosts, err := goodhosts.NewCustomHosts(osHostsFilePath) if err != nil { return err } err = hosts.Add(ip, name) if err != nil { return err } hosts.HostsPerLine(8) err = hosts.Flush() return err ```
kerem closed this issue 2026-03-02 05:08:09 +03:00
Author
Owner

@luthermonson commented on GitHub (Oct 28, 2023):

I thought I had a test for that, I'll check this out today

<!-- gh-comment-id:1783891174 --> @luthermonson commented on GitHub (Oct 28, 2023): I thought I had a test for that, I'll check this out today
Author
Owner

@luthermonson commented on GitHub (Oct 29, 2023):

the good thing is i can recreate the problem and it's being caused by hosts per line being called after every add. this is definitely a bug and ill investigate further but here is how to recreate the problem with just the hostsfile package

// this works
func Test_rfay(t *testing.T) {
	hosts := newHosts()
	for i := 1; i <= 50; i++ {
		hosts.Add("127.0.0.1", fmt.Sprintf("a%d.ddev.site", i))
		
	}
	hosts.HostsPerLine(8) // 8
	fmt.Println(hosts.String())
}
// this does similar to what you showed me
func Test_rfay(t *testing.T) {
	hosts := newHosts()
	for i := 1; i <= 50; i++ {
		hosts.Add("127.0.0.1", fmt.Sprintf("a%d.ddev.site", i))
		hosts.HostsPerLine(8) // 8
	}
	fmt.Println(hosts.String())
}
<!-- gh-comment-id:1784006037 --> @luthermonson commented on GitHub (Oct 29, 2023): the good thing is i can recreate the problem and it's being caused by hosts per line being called after every add. this is definitely a bug and ill investigate further but here is how to recreate the problem with just the hostsfile package ``` // this works func Test_rfay(t *testing.T) { hosts := newHosts() for i := 1; i <= 50; i++ { hosts.Add("127.0.0.1", fmt.Sprintf("a%d.ddev.site", i)) } hosts.HostsPerLine(8) // 8 fmt.Println(hosts.String()) } ``` ``` // this does similar to what you showed me func Test_rfay(t *testing.T) { hosts := newHosts() for i := 1; i <= 50; i++ { hosts.Add("127.0.0.1", fmt.Sprintf("a%d.ddev.site", i)) hosts.HostsPerLine(8) // 8 } fmt.Println(hosts.String()) } ```
Author
Owner

@luthermonson commented on GitHub (Oct 29, 2023):

based on testing you should be fine with the changes in the PR i just posted. i can leave this up as a branch for a bit if you want to test and confirm before we make the merge and release.

thanks again for your help tracking this down

<!-- gh-comment-id:1784019158 --> @luthermonson commented on GitHub (Oct 29, 2023): based on testing you should be fine with the changes in the PR i just posted. i can leave this up as a branch for a bit if you want to test and confirm before we make the merge and release. thanks again for your help tracking this down
Author
Owner

@rfay commented on GitHub (Oct 29, 2023):

I think the current structure was a workaround for

I'll change it to the structure you suggest. Thanks so much!

<!-- gh-comment-id:1784109851 --> @rfay commented on GitHub (Oct 29, 2023): I think the current structure was a workaround for * https://github.com/goodhosts/hostsfile/issues/42 I'll change it to the structure you suggest. Thanks so much!
Author
Owner

@rfay commented on GitHub (Oct 29, 2023):

Oh, I guess I can't. Normally only one host is added at a time, per ddev hostname call.

Here's the code, github.com/ddev/ddev@b71df9536b/pkg/ddevapp/hostname_mgt.go (L127-L147)

Can you see a workaround?

But I see

is on the way, thanks!

<!-- gh-comment-id:1784111200 --> @rfay commented on GitHub (Oct 29, 2023): Oh, I guess I can't. Normally only one host is added at a time, per `ddev hostname` call. Here's the code, https://github.com/ddev/ddev/blob/b71df9536b015854d9ee3cc7829861ef34e0e909/pkg/ddevapp/hostname_mgt.go#L127-L147 Can you see a workaround? But I see * https://github.com/goodhosts/hostsfile/pull/52 is on the way, thanks!
Author
Owner

@luthermonson commented on GitHub (Oct 29, 2023):

https://github.com/goodhosts/hostsfile/releases/tag/v0.1.5 try this and see if youre good

<!-- gh-comment-id:1784147616 --> @luthermonson commented on GitHub (Oct 29, 2023): https://github.com/goodhosts/hostsfile/releases/tag/v0.1.5 try this and see if youre good
Author
Owner

@rfay commented on GitHub (Oct 29, 2023):

Sadly, it has a panic now,

panic: runtime error: index out of range [18] with length 1

goroutine 1 [running]:
github.com/goodhosts/hostsfile.(*Hosts).Add(0x140000ea000, {0x16f82faf4, 0x9}, {0x140000c0130?, 0x1, 0x1})
	/Users/rfay/workspace/ddev/vendor/github.com/goodhosts/hostsfile/hosts.go:179 +0x538
github.com/ddev/ddev/pkg/ddevapp.AddHostEntry({0x16f82fae6, 0xd}, {0x16f82faf4, 0x9})

I didn't change the calling code just updated the go.mod etc.

<!-- gh-comment-id:1784152660 --> @rfay commented on GitHub (Oct 29, 2023): Sadly, it has a panic now, ``` panic: runtime error: index out of range [18] with length 1 goroutine 1 [running]: github.com/goodhosts/hostsfile.(*Hosts).Add(0x140000ea000, {0x16f82faf4, 0x9}, {0x140000c0130?, 0x1, 0x1}) /Users/rfay/workspace/ddev/vendor/github.com/goodhosts/hostsfile/hosts.go:179 +0x538 github.com/ddev/ddev/pkg/ddevapp.AddHostEntry({0x16f82fae6, 0xd}, {0x16f82faf4, 0x9}) ``` I didn't change the [calling code](https://github.com/ddev/ddev/blob/b71df9536b015854d9ee3cc7829861ef34e0e909/pkg/ddevapp/hostname_mgt.go#L127-L147) just updated the go.mod etc.
Author
Owner

@luthermonson commented on GitHub (Oct 29, 2023):

any chance you can ping me on gophers slack?

<!-- gh-comment-id:1784153630 --> @luthermonson commented on GitHub (Oct 29, 2023): any chance you can ping me on gophers slack?
Author
Owner

@luthermonson commented on GitHub (Oct 29, 2023):

i believe me writng all tests to blank Hosts structs covered up a bug, can you vendor from this branch? i don't want to release again if this isn't the fix
https://github.com/goodhosts/hostsfile/pull/53

<!-- gh-comment-id:1784157733 --> @luthermonson commented on GitHub (Oct 29, 2023): i believe me writng all tests to blank Hosts structs covered up a bug, can you vendor from this branch? i don't want to release again if this isn't the fix https://github.com/goodhosts/hostsfile/pull/53
Author
Owner

@luthermonson commented on GitHub (Oct 29, 2023):

confirmed fixed worked via chat outside of git, this is good and being released

<!-- gh-comment-id:1784162336 --> @luthermonson commented on GitHub (Oct 29, 2023): confirmed fixed worked via chat outside of git, this is good and being released
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hostsfile#16
No description provided.