这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(cli): add colors back for cache hit log line #1346

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

Merged
merged 3 commits into from
Jun 8, 2022
Merged

Conversation

tknickman
Copy link
Member

Fixes a bug introduced in 1.2.10 in which the first line (cache hit) is missing the correct color.

Caused by #1256

@vercel
Copy link

vercel bot commented Jun 7, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
turbo-site ✅ Ready (Inspect) Visit Preview Jun 8, 2022 at 4:51PM (UTC)

@@ -60,7 +61,7 @@ func AddFlags(opts *Opts, flags *pflag.FlagSet) {
Value: &logsModeValue{opts: opts},
})
_ = flags.Bool("stream", true, "Unused")
if err := flags.MarkDeprecated("stream", "[WARNING] The --stream flag is unnecesary and has been deprecated. It will be removed in future versions of turbo."); err != nil {
if err := flags.MarkDeprecated("stream", "[WARNING] The --stream flag is unnecessary and has been deprecated. It will be removed in future versions of turbo."); err != nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

typo fix

@@ -796,7 +797,7 @@ func (e *execContext) exec(pt *nodes.PackageTask, deps dag.Set) error {
return nil
}
// Cache ---------------------------------------------
taskCache := e.runCache.TaskCache(pt, hash)
taskCache := e.runCache.TaskCache(pt, hash, e.colorCache)
Copy link
Member Author

Choose a reason for hiding this comment

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

Pass the initialized colorCache instead of creating a new one so we get the same colors.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this stays constant throughout the run, maybe the reference should be on RunCache rather than TaskCache? Or maybe instead of sharing the colorCache, we should be sharing the prefix with the TaskCache?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call, I think the RunCache makes the most sense here.

Copy link
Contributor

@gsoltis gsoltis left a comment

Choose a reason for hiding this comment

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

I definitely like moving the ColorCache into its own package. However the rest of this goes, we should definitely do that.

bufWriter := bufio.NewWriter(output)
if _, err := bufWriter.WriteString(fmt.Sprintf("%s: cache hit, replaying output %s\n", tc.pt.OutputPrefix(), ui.Dim(tc.hash))); err != nil {
if _, err := bufWriter.WriteString(fmt.Sprintf("%s: cache hit, replaying output %s\n", actualPrefix, ui.Dim(tc.hash))); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, are we already baking the color into the log files?

Copy link
Contributor

@nathanhammond nathanhammond Jun 8, 2022

Choose a reason for hiding this comment

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

We are not prebaking this:

logger := log.New(writer, "", 0)

@@ -796,7 +797,7 @@ func (e *execContext) exec(pt *nodes.PackageTask, deps dag.Set) error {
return nil
}
// Cache ---------------------------------------------
taskCache := e.runCache.TaskCache(pt, hash)
taskCache := e.runCache.TaskCache(pt, hash, e.colorCache)
Copy link
Contributor

Choose a reason for hiding this comment

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

If this stays constant throughout the run, maybe the reference should be on RunCache rather than TaskCache? Or maybe instead of sharing the colorCache, we should be sharing the prefix with the TaskCache?

Copy link
Contributor

@nathanhammond nathanhammond left a comment

Choose a reason for hiding this comment

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

This is totally not the place for a feature request, but a feature request: my logs for a particular package are always the same color across runs.

With only five colors available this is likely a bad experience as incidents of mod(int(hash(packageName)), 5) resulting in adjacent duplicate colors will be frequent, but in some future true color terminal world...

Only one review comment is actually intended to block.

@@ -1,4 +1,4 @@
package run
package colorcache
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd maybe go with colorcache.go since that's how we'll see it in code every time.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, the file should technically be named colorcache.go.

@@ -225,8 +227,10 @@ func (tc TaskCache) OutputWriter() (io.WriteCloser, error) {
if err != nil {
return nil, err
}
pref := tc.colorCache.PrefixColor(tc.pt.PackageName)
actualPrefix := pref(tc.pt.OutputPrefix())
Copy link
Contributor

Choose a reason for hiding this comment

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

computedPrefix?

Copy link
Member Author

@tknickman tknickman Jun 8, 2022

Choose a reason for hiding this comment

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

I was using existing verbage to stay consistent -

https://github.com/vercel/turborepo/blob/main/cli/internal/run/run.go#L772

But I do like this better so I'll make this change everywhere.

@@ -225,8 +227,10 @@ func (tc TaskCache) OutputWriter() (io.WriteCloser, error) {
if err != nil {
return nil, err
}
pref := tc.colorCache.PrefixColor(tc.pt.PackageName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Actual strong opinion: pref is wired to "preferences" In my brain, can we maybe call this prefixColor?

Copy link
Member Author

Choose a reason for hiding this comment

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

pref is a function here (given a string, it colors it) - so I might tweak this slightly to be colorPrefixer

bufWriter := bufio.NewWriter(output)
if _, err := bufWriter.WriteString(fmt.Sprintf("%s: cache hit, replaying output %s\n", tc.pt.OutputPrefix(), ui.Dim(tc.hash))); err != nil {
if _, err := bufWriter.WriteString(fmt.Sprintf("%s: cache hit, replaying output %s\n", actualPrefix, ui.Dim(tc.hash))); err != nil {
Copy link
Contributor

@nathanhammond nathanhammond Jun 8, 2022

Choose a reason for hiding this comment

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

We are not prebaking this:

logger := log.New(writer, "", 0)

Copy link
Contributor

@nathanhammond nathanhammond left a comment

Choose a reason for hiding this comment

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

Seems like a winner to me.

@tknickman
Copy link
Member Author

Also @nathanhammond I didn't forget about your feature request. I want that too. But I think I'll tackle that as part of followup. A reliable method to go from task prefix to color would be awesome.

@nathanhammond
Copy link
Contributor

I didn't forget about your feature request

Yeah, but it definitely doesn't belong in this PR. 😅 And Terminal.app isn't true color, so hard to justify...

@@ -1,4 +1,4 @@
package run
package colorcache
Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, the file should technically be named colorcache.go.

@@ -225,8 +228,10 @@ func (tc TaskCache) OutputWriter() (io.WriteCloser, error) {
if err != nil {
return nil, err
}
colorPrefixer := tc.rc.colorCache.PrefixColor(tc.pt.PackageName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably not worth fixing right now, but if we end up revamping how we do coloring and logging, I think it would be good to have this prefix generation done once per task.

We also have a task somewhere to stop baking prefix (and color) into log files, but that's definitely beyond the scope here.

@kodiakhq kodiakhq bot merged commit b2bd1bc into main Jun 8, 2022
@kodiakhq kodiakhq bot deleted the cache_hit_colors branch June 8, 2022 17:17
@gsoltis
Copy link
Contributor

gsoltis commented Jun 8, 2022

@nathanhammond the lines immediately following that log.New call add the color prefix, so we do bake color into the logs. We were just missing it for the direct write to the file that happens before we set up the log.

We should stop writing cache status, our color, and task prefix to the logs.

@nathanhammond
Copy link
Contributor

@gsoltis At first glance it looked like there was already a layer of indirection with a logger and the logstreamers. I assumed that the streamers were consuming and rewriting things written to log for the purpose of stdout. But that is not what they're actually doing underneath the hood.

Agreed that we should tweak behavior, capture the process output 1:1, and then decorate it when we output to stdout (whether new run, replay, or otherwise).

kodiakhq bot pushed a commit that referenced this pull request Jun 21, 2022
Updates the behavior of the `--graph` CLI flag and fixes a few bugs. 

> This command will generate an svg, png, jpg, pdf, json, html, or [other supported output formats](https://graphviz.org/doc/info/output.html) of the current task graph.
The output file format defaults to jpg, but can be controlled by specifying the filename's extension.

> If Graphviz is not installed, or no filename is provided, this command prints the dot graph to `stdout`

This PR also:
1. Updates docs to reflect the current state of the `--graph` CLI flag
1. Refactors the graph visualization code out of `run.go`
1. Cleans up the file name of colors_cache (follow up from #1346)


Fixes #1286
dutterbutter pushed a commit to dutterbutter/docs-turbo that referenced this pull request Nov 1, 2022
Updates the behavior of the `--graph` CLI flag and fixes a few bugs. 

> This command will generate an svg, png, jpg, pdf, json, html, or [other supported output formats](https://graphviz.org/doc/info/output.html) of the current task graph.
The output file format defaults to jpg, but can be controlled by specifying the filename's extension.

> If Graphviz is not installed, or no filename is provided, this command prints the dot graph to `stdout`

This PR also:
1. Updates docs to reflect the current state of the `--graph` CLI flag
1. Refactors the graph visualization code out of `run.go`
1. Cleans up the file name of colors_cache (follow up from vercel/turborepo#1346)


Fixes vercel/turborepo#1286
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