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

Refactor and test the compiler #93

Merged
merged 33 commits into from
May 29, 2022
Merged

Refactor and test the compiler #93

merged 33 commits into from
May 29, 2022

Conversation

matthewmueller
Copy link
Contributor

@matthewmueller matthewmueller commented May 28, 2022

This is a mammoth of a PR that I should have broken up but... ¯\_(ツ)_/¯

The focus of this PR was to:

  1. Add a lot more testing to key pieces of the framework.
  2. Start to simplify and reduce the number of packages with the same name.
  3. Double down on the existing compiler architecture.
  4. Prepare the framework for upcoming features.

User facing changes:

  • bud run --port renamed to bud run --listen
  • Exit codes may have changed (to be tested further)

…local/opt/python@3.8/bin:/Users/m/.cargo/bin:/Users/m/.yarn/bin:/Users/m/.config/yarn/global/node_modules/.bin:/Users/m/Library/Python/2.7/bin:/usr/local/opt/coreutils/libexec/gnubin:/Users/m/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/opt/X11/bin:/Library/Apple/usr/bin:/Users/m/dev/bin:/Users/m/dev/src/github.com/matthewmueller/depot_tools:/usr/local/sbin:/Users/m/.composer/vendor/bin
Copy link
Contributor Author

@matthewmueller matthewmueller left a comment

Choose a reason for hiding this comment

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

Hopefully not such a big PR in the future!

# os.Pipe
- name: Install bud binary into $PATH
run: go install github.com/livebud/bud@latest

Copy link
Contributor Author

@matthewmueller matthewmueller May 29, 2022

Choose a reason for hiding this comment

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

This will go away soon, we'll change v8client to use os.Pipe and the same technique we're using with TCP listeners to pass the file descriptors through the binaries via ExtraFiles. This is mostly setup now, just needs to be refactored in.

RUN git checkout $BUD_VERSION
RUN make install
RUN go install .
RUN bud version
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Helped me track down some Ubuntu-only issues in CI!


```sh
docker build -t bud:latest contributing
docker run -it --rm -v $(PWD):/bud bud /bin/bash
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 allows you to make modifications in a local bud directory and see those changes within the docker container.

cli.Flag("port", "port").String(&cmd.Port).Default("3000")
cli.Run(cmd.Run)
// Run the CLI and wait for the command to finish
func (c *CLI) Run(ctx context.Context, args ...string) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Swapping out the compiler is the meat of this PR.

The previous implementation in internal/bud wasn't tested or setup for testing. Additionally it exposed too many internals as public APIs making some tests pass without actually working top level.

Now there's just one way to build or run a project. Use: err := cli.Run(ctx, args...) and we can focus our energy on making this well-tested, fast, etc.

The CLI itself exposes a lot of optional configuration in its struct for making this package testable.

cli.Args("args").Strings(&c.args).Default(c.args...)
cli.Run(fn)

{ // $ bud create <dir>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only CLI commands in here should be bud commands that are available when we're not in a bud project. The reason for this is we want to be able to define project commands that can even wrap commands like bud build, so the fewer commands we have outside the project the better.

@@ -42,7 +42,7 @@ type Flusher interface {
Flush()
}

type Logger interface {
type Interface interface {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

log.Logger would always default to the standard libary's log package. This fixes that.

// are also fs.ErrNotExists
func (*notExists) Unwrap() error {
return fs.ErrNotExist
}
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 is a surprisingly important fix.

The error chain was getting lost when looking up a file that didn't exist in any filesystem (this can happen when there's errors).

We now collect and continue the chain when we encounter fs.ErrNotExist errors.

The collected errors themselves also satisfy errors.Is(err, fs.ErrNotExist).

func (c *Command) compileAndStart(ctx context.Context, ln net.Listener) (*exe.Cmd, error) {
app, err := c.Project.Compile(ctx, c.Flag)
if err != nil {
func (c *Command) compileAndStart(ctx context.Context, listener socket.Listener) (*exe.Cmd, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved most of the bud {run,build,...} logic into the runtime to prep for wrapping built-in commands.

if err != nil {
console.Error(err.Error())
return nil
}
// TODO: host should be dynamic
console.Info("Ready on http://127.0.0.1" + c.Port)
process = p
Copy link
Contributor Author

Choose a reason for hiding this comment

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

^ Fixes a panic where failures would cause the process to be nil, if the watch command exited, process.Close would panic.

`)
// Test stdio
is.Equal(stdout.String(), "")
is.Equal(stderr.String(), "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lots of tests needed to be refactored to use the new testcli package.

@matthewmueller matthewmueller merged commit 2dfa05a into main May 29, 2022
@matthewmueller matthewmueller deleted the compiler4 branch May 29, 2022 08:30
@matthewmueller matthewmueller mentioned this pull request May 29, 2022
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.

1 participant