Skip to content

Conversation

anthonywu
Copy link

@anthonywu anthonywu commented Jul 8, 2025

Describe your changes

In freeze --version freeze version 0.2.2, I tried to process my local ~/.zprofile and was surprised the file(name) did not auto-detect as Shell.

However, if cat ~/.zprofile | freeze, the lexer was properly detected.

# this fails
freeze ~/.zprofile

   ERROR  Language Unknown

  specify a language with the --language flag

# this works
cat ~/.zprofile | freeze
 WROTE  freeze.png

The behavior difference is that stdin content goes through lexer Analyse, while file path input only goes through Get (and Get then calls Match within its scope). This means the file name is the only consideration.

This smells like a bug/issue that Chroma can fix upstream, but I think there will be other types of file paths (without suffixes, or with wrong suffixes) that could benefit from a last attempt to find a suitable lexer.

This change has no performance degradation and only does the extra Analyse work if/when the Get/Match fails. Ultimately it does the thing that least surprises users, which is that auto-lexers just work.

	if config.Input == "-" || in.IsPipe(os.Stdin) {
		input, err = in.ReadInput(os.Stdin)
		lexer = lexers.Analyse(input)
	} else if config.Execute != "" {
		config.Language = "ansi"
	} else {
		input, err = in.ReadFile(config.Input)
		if err != nil {
			printErrorFatal("File not found", err)
		}
		lexer = lexers.Get(config.Input)
		if lexer == nil {
			// cannot get/match from filename alone
			// pass the content for one more attempt to identify
			lexer = lexers.Analyse(input)
		}
	}

Related issue/discussion:

Checklist before requesting a review

  • I have read CONTRIBUTING.md
  • I have performed a self-review of my code
  • go test -update and go test PASS

If this is a feature

Not a new feature.

@anthonywu anthonywu requested a review from a team as a code owner July 8, 2025 06:58
@anthonywu anthonywu requested review from bashbunni and removed request for a team July 8, 2025 06:58
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