Skip to content

In-place processing of files without mdsh tags progressively deletes lines #98

Description

@mwittie

Bug

When mdsh processes a file in-place that contains no mdsh tags, it corrupts the file on repeated runs by deleting lines from the end.

Reproduction

Build from source

nix develop --command cargo build --release

Case 1: File with trailing newline

Trailing newline stripped on first run, last line deleted on second run

nix develop --command bash -c 'printf "# No mdsh tags\n\ntext\n" > /tmp/test.md && ./target/release/mdsh --input /tmp/test.md && diff <(printf "# No mdsh tags\n\ntext\n") /tmp/test.md'

Run again — "text" is now gone

nix develop --command bash -c './target/release/mdsh --input /tmp/test.md && diff <(printf "# No mdsh tags\n\ntext\n") /tmp/test.md'

Case 2: File without trailing newline

Last line dropped on first run

nix develop --command bash -c 'printf "# No mdsh tags\n\ntext" > /tmp/test2.md && ./target/release/mdsh --input /tmp/test2.md && diff <(printf "# No mdsh tags\n\ntext") /tmp/test2.md'

Root cause

Two interacting issues in src/main.rs:

  1. trim_ascii_end (applied on in-place writes) strips all trailing ASCII whitespace including \n, removing the file's trailing newline.
  2. The parser's catch-all in src/parser.rs matches lines with (take_until("\n"), newline), which requires a trailing \n. Lines without one are silently dropped.

This creates a cascading failure: each run strips the trailing newline, causing the next run to drop the now-unterminated last line.

Expected behavior

Running mdsh on a file with no mdsh tags should leave it unchanged, regardless of whether it has a trailing newline.

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