Skip to content

fix return value of os.writefile_ifnotequal()#2667

Draft
mercury233 wants to merge 4 commits intopremake:masterfrom
mercury233:patch-2
Draft

fix return value of os.writefile_ifnotequal()#2667
mercury233 wants to merge 4 commits intopremake:masterfrom
mercury233:patch-2

Conversation

@mercury233
Copy link
Copy Markdown
Contributor

@mercury233 mercury233 commented Apr 27, 2026

What does this PR do?

  • fix return value of os.writefile_ifnotequal()
  • add test for that function
  • update the document for that function

How does this PR change Premake's behavior?

Before #2654 , os.writefile_ifnotequal() returns 1 if successful, 0 if the string is identical to the current file contents, otherwise -1 and an error message. #2654 seems to changed this behavior accidentally, return 0 on success too. This PR change the behavior back.

Anything else we should know?

@jkriegshauser

Did you check all the boxes?

  • Focus on a single fix or feature; remove any unrelated formatting or code changes
  • Add unit tests showing fix or feature works; all tests pass
  • Mention any related issues (put closes #XXXX in comment to auto-close issue when PR is merged)
  • Follow our coding conventions
  • Minimize the number of commits
  • Align documentation to your changes

You can now support Premake on our OpenCollective. Your contributions help us spend more time responding to requests like these!

- Change the behavior back
- Update the document
### Return Value ###

True if successful, otherwise nil and an error message.
`1` if successful, `0` if the string is identical to the current file contents, otherwise `-1` and an error message.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest changing this to be a table

Comment thread src/host/os_writefile_ifnotequal.c
@jkriegshauser
Copy link
Copy Markdown
Contributor

Please add unit tests to prevent future regressions

### Return Value ###

True if successful, otherwise nil and an error message.
`1` if successful, `0` if the string is identical to the current file contents, otherwise `-1` and an error message.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`1` if successful, `0` if the string is identical to the current file contents, otherwise `-1` and an error message.
The first return value:
| Value | Explanation |
|-------|------------------------------------------------------|
| 1 | The string was written to the file |
| 0 | The string is identical to the current file contents |
| -1 | An error occurred |
The second return value is an error message if the first return value is -1, otherwise nil.

should it be like this?

@mercury233 mercury233 marked this pull request as draft April 28, 2026 00:56
@mercury233
Copy link
Copy Markdown
Contributor Author

Added tests, but I’m unsure whether they are appropriate, since the test_runner.lua intentionally uses functions like stub_os_writefile_ifnotequal to prevent actual operations.

@jkriegshauser
Copy link
Copy Markdown
Contributor

Added tests, but I’m unsure whether they are appropriate, since the test_runner.lua intentionally uses functions like stub_os_writefile_ifnotequal to prevent actual operations.

If the test fails before your fix then it's appropriate 😄

You may have to capture the original function value before it's stubbed out. Such as what I did in test_lua_unicode.lua:

    local real_io_open = io.open

-- ...

function suite.loadfile_UnicodeFilename()
	local p = tmpname()
	p = p .. "_café.lua"
	local f = assert(real_io_open(p, "w"))
	-- ...
end

@mercury233 mercury233 mentioned this pull request Apr 30, 2026
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants