Skip to content

BUG: mktemp “File exists” error on macOS when running spin lint multiple times #29545

@math-hiyoko

Description

@math-hiyoko

Describe the issue:

On macOS, running spin lint multiple times in the same working directory sometimes causes the following error during the C API borrow-reference linter step
It appears that this issue originates from the following line in numpy/tools/ci/check_c_api_usage.sh:

OUTPUT=$(mktemp .tmp/c_api_usage_report.XXXXXX.txt)

However, according to the mktemp(1) man page on macOS, the XXXXXX template must be the last part of the filename in order to be substituted properly. Since the current template ends with .txt, the XXXXXX is not recognized and is left unchanged, causing repeated executions to attempt writing to the same filename (.tmp/c_api_usage_report.XXXXXX.txt), which already exists.

$ man mktemp
MKTEMP(1)                         General Commands Manual                         MKTEMP(1)

NAME
     mktemp – make temporary file name (unique)

SYNOPSIS
     mktemp [-d] [-p tmpdir] [-q] [-t prefix] [-u] template ...
     mktemp [-d] [-p tmpdir] [-q] [-u] -t prefix

DESCRIPTION
     The mktemp utility takes each of the given file name templates and overwrites a
     portion of it to create a file name.  This file name is unique and suitable for use by
     the application.  The template may be any file name with some number of ‘Xs’ appended
     to it, for example /tmp/temp.XXXX.  The trailing ‘Xs’ are replaced with the current
     process number and/or a unique letter combination.  The number of unique file names
     mktemp can return depends on the number of ‘Xs’ provided; six ‘Xs’ will result in
     mktemp selecting 1 of 56800235584 (62 ** 6) possible file names.  On case-insensitive
     file systems, the effective number of unique names is significantly less; given six
     ‘Xs’, mktemp will instead select 1 of 2176782336 (36 ** 6) possible unique file names.

     If mktemp can successfully generate a unique file name, the file is created with mode
     0600 (unless the -u flag is given) and the filename is printed to standard output.

Reproduce the code example:

spin lint

Error message:

Running Ruff Check...
All checks passed!

Running C API borrow-reference linter...
Scanning      549 C/C++ source files...
mktemp: mkstemp failed on .tmp/c_api_usage_report.XXXXXX.txt: File exists

Python and NumPy Versions:

main branch 3049f4e

Runtime Environment:

% sw_vers
ProductName:		macOS
ProductVersion:		15.2
BuildVersion:		24C101

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions