Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[direnv] Add basic .envrc.example #4747

Merged
merged 15 commits into from
Jan 3, 2024
Merged

[direnv] Add basic .envrc.example #4747

merged 15 commits into from
Jan 3, 2024

Conversation

zmt
Copy link
Contributor

@zmt zmt commented Dec 20, 2023

Pull Request check list

  • Commit conforms to CONTRIBUTING.md
  • Proper tests/regressions included
  • Documentation updated

Affected functionality
Add a basic .envrc.example that will build the go sdk using the Makefile and
setup the environment to use it so developers who use direnv
can easily "automatically" use the managed go sdk. Update CONTRIBUTING.md to
add a section describing the intended usage of direnv .envrc.example.

Description of change
While developing, it is good to use the same go tool as the make targets
when running go test. This makes that easy for users of direnv.
Developers who don't use direnv or don't copy the example into place will
see no change.

Signed-off-by: Zack Train <ztrain@uber.com>
@zmt
Copy link
Contributor Author

zmt commented Dec 20, 2023

My .envrc.local has:

go install golang.org/x/tools/gopls@latest

@zmt
Copy link
Contributor Author

zmt commented Dec 20, 2023

I think I should add the make go-check at the top to avoid:

% cd spire
direnv: loading .envrc
direnv: loading .envrc.local
go: cannot find GOROOT directory: /home/user/gocode/src/github.com/spiffe/spire/.build/linux-x86_64/go/1.21.5
direnv: export ~GOROOT ~PATH

when the worktree is completely clean (git -d -f -f -x)

zmt added 5 commits December 20, 2023 22:04
Signed-off-by: Zack Train <ztrain@uber.com>
Signed-off-by: Zack Train <ztrain@uber.com>
Signed-off-by: Zack Train <ztrain@uber.com>
Signed-off-by: Zack Train <ztrain@uber.com>
@azdagron
Copy link
Member

Hey @zmt, thanks for opening this! I'm all about making contributor lives simpler though I'm a little hesitant introducing this to the repository, since it provides a place for changes to slip in that can run on developer machines without consent once the initial directory has been initially allow-listed. The makefile has targets that can be used to compile/build/test with the expected toolchain. Is there something that we get with this that we don't get with the Makefile?

@zmt
Copy link
Contributor Author

zmt commented Dec 21, 2023

Is there something that we get with this that we don't get with the Makefile?

I find it quicker/better/faster when iterating to use go test directly within single packages rather than going back to top-level and make test of everything. I want to be sure the go test will be equivalent to what make test is doing. This gives me that automagically. I also find it useful for go install (per my .envrc.local for gopls) and go get. Without this, I have to manually do all the stuff it does.

CONTRIBUTING.md Outdated Show resolved Hide resolved
Signed-off-by: Zack Train <ztrain@uber.com>
@zmt
Copy link
Contributor Author

zmt commented Jan 2, 2024

since it provides a place for changes to slip in that can run on developer machines without consent once the initial directory has been initially allow-listed

I triple-checked this after our discussion. You are correct about the source_env .envrc.local: https://direnv.net/man/direnv-stdlib.1.html#codesourceenv-ltfileordirpathgtcode. It just auto-reloads on changes as long as the .envrc is unchanged.

sigh

@zmt zmt marked this pull request as draft January 2, 2024 20:40
@zmt
Copy link
Contributor Author

zmt commented Jan 2, 2024

I guess if I remove the hook to source_env arbitrary code, it might be more acceptable, but maybe I should just withdraw this.

@zmt
Copy link
Contributor Author

zmt commented Jan 2, 2024

Related:
direnv/direnv#556
direnv/direnv#448

zmt added 2 commits January 2, 2024 22:15
See potential direnv security improvements in
direnv/direnv#556.
Signed-off-by: Zack Train <ztrain@uber.com>
Signed-off-by: Zack Train <ztrain@uber.com>
@zmt zmt marked this pull request as ready for review January 2, 2024 22:32
@zmt zmt changed the title [direnv] Add basic .envrc [direnv] Add basic .envrc.example Jan 2, 2024
.envrc.example Outdated
@@ -0,0 +1,19 @@
toplevel="$(git rev-parse --show-toplevel)"

# ensure the pre-commit git hook is in place
Copy link
Member

Choose a reason for hiding this comment

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

Can omit setting up the hook now

.envrc.example Outdated
Comment on lines 9 to 11
pushd $toplevel >/dev/null &&
make go-check &&
popd >/dev/null
Copy link
Member

Choose a reason for hiding this comment

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

Feels a little cleaner to just use a subshell? (pushd and popd are verbose)

Suggested change
pushd $toplevel >/dev/null &&
make go-check &&
popd >/dev/null
(cd "$toplevel"; make go-check)

Or, preferably, we can leverage the -C option with make:

Suggested change
pushd $toplevel >/dev/null &&
make go-check &&
popd >/dev/null
make -C "$toplevel" go-check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah - I make extensive use of git -C because I manage my home dir as a git repo, but it slipped my mind to do the same with make.

.envrc.example Outdated
machine="$(go env GOOS)-$(uname -m)"

# export GOROOT and PATH for managed go sdk
GOROOT="$toplevel/.build/$machine/go/$(cat .go-version)"
Copy link
Member

Choose a reason for hiding this comment

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

Why is GOROOT required? This is typically deduced by where the go binary resides. If PATH causes the right go binary to be executed, then GOROOT shouldn't be required?
It might be useful however to unset GOROOT....

Copy link
Contributor Author

@zmt zmt Jan 2, 2024

Choose a reason for hiding this comment

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

It might be useful however to unset GOROOT....

I started with unset, but I think I had an issue installing gopls in an unexpected spot, so I set it, but am perfectly happy to move it back to just unset it.

Copy link
Member

Choose a reason for hiding this comment

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

Do you have any details on the failure? If not, I'd rather go with the unset route until we do (FYI, our makefile unset's GOROOT)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am aware that the Makefile unsets GOROOT. I think I just futzed about until the first thing worked for me, but didn't fully clean it up. I don't actually need GOROOT. Updating.

.envrc.example Outdated

# export GOROOT and PATH for managed go sdk
GOROOT="$toplevel/.build/$machine/go/$(cat .go-version)"
PATH="$GOROOT/bin:$PATH"
Copy link
Member

Choose a reason for hiding this comment

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

There is already a makefile target that prints a modified path with the correct toolchain directory added (no need to repeat the same magic the makefile does to determine the path):

Suggested change
PATH="$GOROOT/bin:$PATH"
PATH="$(make -C "$toplevel" go-bin-path)"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs --no-print-directory to work.

.envrc.example Outdated
# export GOROOT and PATH for managed go sdk
GOROOT="$toplevel/.build/$machine/go/$(cat .go-version)"
PATH="$GOROOT/bin:$PATH"
export GOROOT PATH
Copy link
Member

Choose a reason for hiding this comment

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

nit: PATH should already be exported.

Signed-off-by: Zack Train <ztrain@uber.com>
.envrc.example Outdated
unset GOROOT
make -C "$toplevel" go-check
go_bin_path="$(make --no-print-directory -C $toplevel go-bin-path)"
PATH="$go_bin_path:$PATH"
Copy link
Member

Choose a reason for hiding this comment

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

make go-bin-path already appends the rest of the PATH so suffixing with $PATH shouldn't be required

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! No wonder the path looked so long.

Signed-off-by: Zack Train <ztrain@uber.com>
.envrc.example Outdated
# build and use the managed go sdk
unset GOROOT
make -C "$toplevel" go-check
PATH="$(make --no-print-directory -C $toplevel go-bin-path)"
Copy link
Member

Choose a reason for hiding this comment

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

We should quote toplevel in case there are spaces to prevent word splitting

Suggested change
PATH="$(make --no-print-directory -C $toplevel go-bin-path)"
PATH="$(make --no-print-directory -C "$toplevel" go-bin-path)"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got it in one spot but not the other 😞

Signed-off-by: Zack Train <ztrain@uber.com>
Copy link
Member

@azdagron azdagron left a comment

Choose a reason for hiding this comment

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

\o/

@MarcosDY MarcosDY added this to the 1.9.0 milestone Jan 3, 2024
@MarcosDY MarcosDY merged commit 990bbc3 into spiffe:main Jan 3, 2024
32 checks passed
sriyer pushed a commit to spire-vault/spire that referenced this pull request Feb 23, 2024
Add a basic .envrc.example that will build the go sdk using the Makefile and
setup the environment to use it so developers who use direnv
can easily "automatically" use the managed go sdk. Update CONTRIBUTING.md to
add a section describing the intended usage of direnv .envrc.example.

Signed-off-by: Zack Train <ztrain@uber.com>
rushi47 pushed a commit to rushi47/spire that referenced this pull request Apr 11, 2024
Add a basic .envrc.example that will build the go sdk using the Makefile and
setup the environment to use it so developers who use direnv
can easily "automatically" use the managed go sdk. Update CONTRIBUTING.md to
add a section describing the intended usage of direnv .envrc.example.

Signed-off-by: Zack Train <ztrain@uber.com>
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