github/codeql-action-sync-tool

๐Ÿ› Error running with an empty cache dir: mkdir x file exists

marcellodesales opened this issue ยท 2 comments

I can't get to run a sync with a directory previously created from a Cache dir... The following error message is how it terminates...

Requirement

  • Cache the sync dir externally
    • That is, an initial empty dir is required to create the cache
    • The existing version fails to check for empty dir and create a cache file

Here's the error:

Caution

mkdir /var/tmp/codeqltmp: file exists\nCould not create cache directory.

๐ŸŠ Faulty Code?

We should have first a check if the directory exists and it's empty...

  • We are missing a check if it exists and it's empty, then we should skip the creation of the dir...

  • if isEmptyOrNonExistent {
    err := os.Mkdir(cacheDirectory.path, 0755)
    if err != nil {
    return errors.Wrap(err, "Could not create cache directory.")
    }
    err = ioutil.WriteFile(cacheVersionFilePath, []byte(version), 0644)
    if err != nil {
    return errors.Wrap(err, "Could not create cache version file.")
    }
    return nil
    }

๐Ÿ”ง Required Workflow

  • Since this is for using the remote cache, I'd like to prepare the local cache
    • That is, its creation is before the execution
    • The directory will be empty anyway
      - name: Create cache directory
        run: sudo mkdir -p ${{ env.CACHE_DIR }} && sudo chmod 777 ${{ env.CACHE_DIR }}

      - name: Restore cache
        uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_DIR }}
          key: ${{ runner.os }}-codeql-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-codeql-
      - name: Run sync tool
        run: |
          ./codeql-action-sync sync \
            --cache-dir "${{ env.CACHE_DIR }}" \
            --destination-url "${{ env.GH_ENTERPRISE_URL }}" \
            --destination-repository ${{ inputs.githubOrRepo }} \
            --destination-token "${{ env.GH_ENTERPRISE_TOKEN }}"

๐Ÿ’ป Logs

  • This is the error code trying to run it from actions
Run ./codeql-action-sync sync \
time="2025-01-28T20:22:55Z" level=info msg="Starting CodeQL Action sync tool version 1.0.11..."
time="2025-01-28T20:22:55Z" level=fatal msg="mkdir /var/tmp/codeqltmp: file exists\nCould not create cache directory.\ngithub.com/github/codeql-action-sync/internal/cachedirectory.(*CacheDirectory).CheckOrCreateVersionFile\n\t/home/runner/work/codeql-action-sync-tool/codeql-action-sync-tool/internal/cachedirectory/cachedirectory.go:87\ngithub.com/github/codeql-action-sync/internal/pull.Pull\n\t/home/runner/work/codeql-action-sync-tool/codeql-action-sync-tool/internal/pull/pull.go:264\ngithub.com/github/codeql-action-sync/cmd.glob..func4\n\t/home/runner/work/codeql-action-sync-tool/codeql-action-sync-tool/cmd/sync.go:17\ngithub.com/spf[13](https://git.viasat.com/seceng-devsecops-platform/vionix-platform-ghas-code_scanning_updater/actions/runs/2730506/job/10022602#step:8:14)/cobra.(*Command).execute\n\t/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:985\ngithub.com/spf13/cobra.(*Command).ExecuteC\n\t/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1117\ngithub.com/spf13/cobra.(*Command).Execute\n\t/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1041\ngithub.com/spf13/cobra.(*Command).ExecuteContext\n\t/home/runner/go/pkg/mod/github.com/spf13/cobra@v1.8.1/command.go:1034\ngithub.com/github/codeql-action-sync/cmd.Execute\n\t/home/runner/work/codeql-action-sync-tool/codeql-action-sync-tool/cmd/root.go:76\nmain.main\n\t/home/runner/work/codeql-action-sync-tool/codeql-action-sync-tool/main.go:15\nruntime.main\n\t/opt/hostedtoolcache/go/1.21.6/x64/src/runtime/proc.go:267\nruntime.goexit\n\t/opt/hostedtoolcache/go/1.21.6/x64/src/runtime/asm_amd64.s:1650"

Hope to get this ticket verified after the PR #123 has been merged!

Thanks for the fix here. It looks like it works.