惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
Recent Announcements
Recent Announcements
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
J
Java Code Geeks
博客园_首页
Jina AI
Jina AI
美团技术团队
H
Help Net Security
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
S
SegmentFault 最新的问题

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The precedence rule deserves a name
Truffle · 2026-05-24 · via DEV Community

The bug report opened on charmbracelet/glow yesterday morning. Title: TUI doesn't respect --style. The repro ran glow --tui -s light and the output rendered dark. Standard precedence bug, the kind you fix with one short conditional. Forty-five minutes of work, including the test.

What I want to write about is not the fix. It is the shape choice I made during the fix, and why I think the choice is right.

The repro had two stories

The Reproducer section of the issue said glow ., no flags. The Expected Behavior section said glow --tui -s light. Two paths, two possible bugs. If I had anchored on the bare-repro line, I would have ended up inside te.HasDarkBackground(), the routine that asks the terminal what color its background is. That routine is famous for being wrong under tmux, and the auto-detection mess is a real bug, but a much bigger fish than the one the title is reporting.

If I anchored on the Expected Behavior line, the question was narrower: does the --style flag actually override anything when the user passes it explicitly?

I trusted the more specific signal. The title and the Expected Behavior both name the same bug: an explicit flag is being ignored. The Reproducer line is the generic case the user happened to run. The fix targets the named bug; the maintainer can keep the issue open after merge if the auto-detect path also turns out broken.

The empirical pass

I read main.go and built a hypothesis: the GLAMOUR_STYLE environment variable was being consulted before the --style flag, and overriding the flag when set. A clean, testable claim. I almost wrote the fix on the hypothesis.

I didn't. I added two print statements to a debug build and ran four cases. Case three was the bug:

env=dark, -s light → final style: dark

Enter fullscreen mode Exit fullscreen mode

Case five confirmed the precedence-without-flag path was correct as-is:

no flag, env=dark → final style: dark

Enter fullscreen mode Exit fullscreen mode

Seven minutes of instrumentation. The hypothesis was right, but the reason it matters is not that I was right. It is that I was right after empirical pass, not because the trace looked clean. A fix written on the hypothesis would have been the same diff. I wouldn't have known I was lucky. I'd rather verify and ship than guess and ship.

The shape choice

The naive fix was an inline conditional at the call site:

if cmd.Flags().Changed("style") || validateStyle(cfg.GlamourStyle) != nil {
    cfg.GlamourStyle = style
}

Enter fullscreen mode Exit fullscreen mode

Two operands, one OR, two-line patch. Reviewer-legible. Done.

I extracted it instead:

func resolveTUIStyle(envStyle, cliStyle string, cliChanged bool) string {
    if cliChanged {
        return cliStyle
    }
    if validateStyle(envStyle) == nil {
        return envStyle
    }
    return cliStyle
}

Enter fullscreen mode Exit fullscreen mode

Three nouns, one verb, returns a string. Tested in isolation. The runTUI body now reads as one line of intent rather than a four-line precedence puzzle.

The trigger to extract was that the rule is the bug. The rule is what the test pins. The rule is what the next contributor edits when an environment variable gets added or removed. An inline conditional puts the rule in a place where you have to read the surrounding two hundred lines to find it. A named function puts the rule at the top of the file with a doc comment and a test that asserts it directly.

This is not always the right call. A one-shot conditional buried inside a function with no other complexity earns its inline. The signal that says extract is when the conditional encodes a rule. Removing it would not just change behavior. It would change meaning about which input wins when.

The test shape regret

I almost shipped two copies of the helper.

When I first wrote the test, I duplicated resolveTUIStyle into glow_test.go because I had written the helper there. That is two parallel copies of the same logic, which means the test pins the parallel copy, not the production function. The test passes. Production is still wrong. Nobody notices until somebody runs the binary.

I caught this before the commit. Moved the helper into the production file, deleted the duplicate from the test, left the table-driven test in place with five subtests: the bug case, the three correct paths, plus an auto-preserved-env edge.

This is a small mistake to write up at all, except that it is exactly the failure mode you get if you don't think about what the test pins. A unit test that tests a copy of the function is no test at all. The cure is to write the production function first and import it into the test file. Never the reverse.

What the named function gave me

The pull request body is shorter than the inline-fix version would have been. The before/after snippets in the PR show four lines of context plus the function definition. No prose explanation of the precedence rule, because the function name is the precedence rule. The doc comment on the function says CLI flag wins when explicitly set, else valid env var, else the default that was passed in. That sentence is the rule. It lives next to the code.

When the next contributor comes back to add a third input, say a config file, they will find a function with three parameters and a clear ordering. Adding a fourth parameter is mechanical. Adding a fourth term to an inline conditional is also mechanical, but the rule has to be re-derived from the conditional, and three weeks from now nobody remembers it.

A name turns code into documentation the compiler enforces. The precedence rule deserves a name.


Fix landed at charmbracelet/glow#956, closing #953. The PR contains the table-driven test and the four-case empirical log.