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

feat: run local --watch flag #5413

Merged
merged 28 commits into from
Nov 7, 2023

Conversation

CaptainCarpensir
Copy link
Contributor

Implements the --watch flag for run local which watches your copilot workspace for file changes and restarts the docker containers when you make changes.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.

@CaptainCarpensir CaptainCarpensir requested a review from a team as a code owner October 26, 2023 21:32
@CaptainCarpensir CaptainCarpensir requested review from Lou1415926 and removed request for a team October 26, 2023 21:32
@github-actions
Copy link

github-actions bot commented Oct 26, 2023

🍕 Here are the new binary sizes!

Name New size (kiB) size (kiB) Delta (%)
macOS (amd) 52332 51976 +0.68
macOS (arm) 53176 52812 +0.69
linux (amd) 46028 45716 +0.68
linux (arm) 45316 45060 +0.57
windows (amd) 43480 43168 +0.72


// ensure that containers are fully stopped after o.stopTask finishes blocking
// TODO(Aiden): Implement a container ID system or use `docker ps` to ensure containers are stopped
time.Sleep(1 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

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

ah, what error do you see otherwise - something like "xx container is already running"? Is the "xx" consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We'd see the following error:

[front-end] docker: Error response from daemon: Conflict. The container name "/basic-test-front-end-front-end" is already in use by container "1e49e6b3407f538ac362fd29607c9e38a1ed0309aae2a455a8ee63eea6e38b25". You have to remove (or rename) that container to be able to reuse that name.
[front-end] See 'docker run --help'.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Danny said we could fix this by running docker ps to check when it's stopped but I think that tagging containers with a random id would be simpler and easier to implement. I can see that maybe users expect their containers to be named the same as it would be on ECS though.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah I see, docker stop returns after the container is stopped, but this doesn't rm the container. To do this, docker rm is probably required, which is automatically run (because we've started the container by docker run --rm iirc) but not waited for.

Both docker ps -a and random container name (maybe by appending random string) sound like good solutions that are worth further considerations!

Copy link
Contributor

Choose a reason for hiding this comment

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

with a random id would be simpler and easier to implement.

probably yes - with the downside of polluting someone's docker engine with a bunch of tags.

@codecov-commenter
Copy link

codecov-commenter commented Oct 26, 2023

Codecov Report

Attention: 58 lines in your changes are missing coverage. Please review.

Comparison is base (e5aebd9) 69.99% compared to head (6349e0c) 69.97%.

❗ Current head 6349e0c differs from pull request most recent head dd9dd0b. Consider uploading reports for the commit dd9dd0b to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##           mainline    #5413      +/-   ##
============================================
- Coverage     69.99%   69.97%   -0.03%     
============================================
  Files           299      301       +2     
  Lines         45641    45789     +148     
  Branches        295      295              
============================================
+ Hits          31946    32039      +93     
- Misses        12148    12189      +41     
- Partials       1547     1561      +14     
Files Coverage Δ
internal/pkg/cli/flag.go 90.47% <ø> (ø)
internal/pkg/docker/orchestrator/orchestrator.go 93.79% <0.00%> (-0.35%) ⬇️
internal/pkg/cli/file/hidden.go 0.00% <0.00%> (ø)
internal/pkg/cli/file/watch.go 68.42% <68.42%> (ø)
internal/pkg/cli/run_local.go 66.42% <67.54%> (-0.96%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@iamhopaul123 iamhopaul123 left a comment

Choose a reason for hiding this comment

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

Have you looked into https://github.com/rjeczalik/notify 🤔 I wonder what the benefit would be for implementing our own wrapper to get a recursive watcher, given the fact that we'll introduce a 3rd party package anyway.

internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
internal/pkg/cli/file/hidden.go Show resolved Hide resolved
internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
@CaptainCarpensir
Copy link
Contributor Author

CaptainCarpensir commented Oct 27, 2023

Have you looked into https://github.com/rjeczalik/notify 🤔 I wonder what the benefit would be for implementing our own wrapper to get a recursive watcher, given the fact that we'll introduce a 3rd party package anyway.

Will look into it, there's no benefit to writing our own wrapper as far as I know, I just didn't find any existing packages that did this well.

internal/pkg/cli/file/watch.go Show resolved Hide resolved
internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
internal/pkg/cli/file/hidden.go Show resolved Hide resolved
if info.IsDir() {
switch event.Op {
case fsnotify.Create:
err := rw.Add(event.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like event.Name is relative to the "input" (https://pkg.go.dev/github.com/fsnotify/fsnotify#Event). Does rw.Add expects an absolute 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.

Nope, rw.Add calls the underlying Add function in the fsnotify.Watcher. This function can be both absolute or relative. I think this can only become an issue if the relative position being called from changes. This should error out though, and because --watch programmatically calls this, it's just reusing the directory provided by ws.Path(), so we'll never run into this issue here anyways.

internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
@CaptainCarpensir CaptainCarpensir marked this pull request as draft October 30, 2023 22:23
@CaptainCarpensir
Copy link
Contributor Author

Converted to draft to write local integration tests for watch.go

@CaptainCarpensir CaptainCarpensir marked this pull request as ready for review November 3, 2023 17:26
internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
internal/pkg/cli/run_local.go Show resolved Hide resolved
internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
Copy link
Contributor

@KollaAdithya KollaAdithya left a comment

Choose a reason for hiding this comment

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

LGTM! :shipit:

I assume TODO's are going to be finished after the release 🤔

internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
@CaptainCarpensir
Copy link
Contributor Author

I assume TODO's are going to be finished after the release 🤔

Dockerignore file blacklist definitely won't be finished before this release. I don't think properly waiting for docker rm to finish will be able to either but if I have time I think I could implement it. The main blocker for that one is just deciding which of the two solutions is a better approach. For now I'm going to assume that both will be implemented for v1.33.

Copy link
Contributor

@iamhopaul123 iamhopaul123 left a comment

Choose a reason for hiding this comment

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

:shipit:

"github.com/stretchr/testify/require"
)

func TestRecursiveWatcher(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome tests!

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package filetest
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we use this?

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 it's used in run_local_test.go but maybe the package name should be changed.

@CaptainCarpensir CaptainCarpensir added do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. and removed do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. labels Nov 7, 2023
internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
internal/pkg/cli/file/hidden.go Outdated Show resolved Hide resolved
internal/pkg/cli/file/watch.go Outdated Show resolved Hide resolved
@@ -223,6 +237,10 @@ func newRunLocalOpts(vars runLocalVars) (*runLocalOpts, error) {
}
return containerURIs, nil
}
o.debounceTime = 5 * time.Second
Copy link
Contributor

Choose a reason for hiding this comment

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

why does this need to be a field of runLocalOpts 👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For tests to set the debounce time to 0

internal/pkg/cli/run_local.go Outdated Show resolved Hide resolved
@mergify mergify bot merged commit 90d3af3 into aws:mainline Nov 7, 2023
12 checks passed
@CaptainCarpensir CaptainCarpensir deleted the cli/run-local-watch-flag branch November 13, 2023 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants