Skip to content

wt reads a config that it cannot open as an empty config #26

Open

Description

@lorenzolfm

Describe the bug

Config::load asks Path::exists first. exists is false for each error, not only for a file that is not there. So a link loop, a chain of more than 40 links, or a directory that wt cannot search gives an empty config, and wt says nothing.

// src/config.rs
let path = Self::path()?;
if !path.exists() {
    return Ok(Config::default());
}

The hook then makes no links, wt sync says "each worktree is correct", and wt ls prints no links. wt share moves the path into the store and makes the links, and only then save refuses the config. The store then has a path that the config does not give (#25).

load and save also disagree about the same file. save refuses a loop with "is a chain of more than 40 links", and load reads it as empty.

Steps to Reproduce

  1. In a repository where wt share .env worked, replace the config with a link to itself: mv ~/.config/wt/config.toml ~/wt-config.toml && ln -s config.toml ~/.config/wt/config.toml.

  2. wt sync prints "each worktree is correct", and wt ls prints no links.

  3. wt share data moves data into the store and links it, and then it fails:

      moved      data -> shared/data
      linked     repo
    wt: .../.config/wt/config.toml is a chain of more than 40 links
    

Expected behavior

Only a file that is not there is no config. Each other error stops the command and names the path, as save does.

Proposed fix

Read the file, and treat only NotFound as no config. inspect in src/seed.rs already does this.

pub fn load() -> Result<Config> {
    let path = Self::path()?;
    let text = match fs::read_to_string(&path) {
        Ok(text) => text,
        // A link to a file that the dotfiles do not have yet is also absent.
        Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(Config::default()),
        Err(e) => return Err(e).with_context(|| format!("wt cannot read {}", path.display())),
    };
    toml::from_str(&text)
        .with_context(|| format!("wt cannot read the format of {}", path.display()))
}

A link to a file that is not there still gives NotFound, so it is still no config, as #23 wants. This also drops one stat, and the gap between exists and the read.

To test it without XDG_CONFIG_HOME, I would split load into Config::path() and read(path), as save calls write_in_one_step(path).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions