Skip to content

analyze truncates an existing -o database when the input matches no files #138

Description

@SkowronskiAndrew

When the input paths match no files at all, analyze still creates the output database before
discovering there is nothing to do. SQLiteWriter.Begin truncates the file (File.WriteAllBytes)
and runs the full schema init, the run then finds zero files, and the (now empty) database is
deleted.

The destructive part is the truncation. A mistyped path is enough:

UnityDataTool analyze C:/typo/not-a-real-path -o important.db

important.db is a previous, valid analysis. The command prints
Warning: path not found, skipping: C:/typo/not-a-real-path, truncates important.db anyway, and
(since #115) deletes it. The old content is gone either way — this behaviour predates #115, which
only changed what is left behind afterwards.

Nothing needs to touch the filesystem in this case. A check before the database is created, e.g.
after CollectFiles() in AnalyzerTool.Analyze:

if (files.Count == 0)
{
    Console.Error.WriteLine("Error: the input paths matched no files to analyze.");
    return 1;
}

avoids creating or truncating anything, and skips the schema init and finalize work that is
currently thrown away.

Points to decide:

  • This introduces a second "nothing was analyzed" message alongside the one added in analyze: exit code is 0 even when nothing was successfully processed #115. They
    could share wording.
  • It also makes an existing database at -o survive this class of failure, while a run where every
    file failed still deletes it. That difference is defensible (the file is only removed once its
    content has already been destroyed) but it should be a deliberate choice.

Found while reviewing the fix for #115; left out of that PR to keep it to a single failure 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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions