Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUILD FAILURE] Bazel builds are always failing! emsdk (3.1.31) #1181

Closed
alienself opened this issue Feb 1, 2023 · 8 comments · Fixed by #1326
Closed

[BUILD FAILURE] Bazel builds are always failing! emsdk (3.1.31) #1181

alienself opened this issue Feb 1, 2023 · 8 comments · Fixed by #1326

Comments

@alienself
Copy link

alienself commented Feb 1, 2023

Hi,

I am having an issue with Bazel and emscripten.
I simply want to link a static library which builds fine natively (using cc_binary directly) but always fails with wasm_cc_binary:
I am using the latest version of emsdk (3.1.31)

I tried to remove all the bazel folders and even started from scratch using the hello world example (https://github.com/emscripten-core/emsdk/tree/main/bazel/hello-world).

As soon as I add deps to cc_binary the emscripten build fails!

The error

# Execution platform: @local_config_platform//:host
Traceback (most recent call last):
  File "C:\Users\admin\_bazel_admin\m6eps4yl\execroot\hello_world\external\emsdk\emscripten_toolchain\link_wrapper.py", line 64, in <module>
    os.rename(output_file, output_file + '.' + oformat)
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'bazel-out/wasm-opt-ST-b4dbd4a2bfc4/bin/hello-world/hello-world' -> 'bazel-out/wasm-opt-ST-b4dbd4a2bfc4/bin/hello-world/hello-world.js'

Files

/hello-world
    /BUILD
    /hello-world.cc
/lib
    /localtime
        /localtime.cpp
        /localtime.h
        /BUILD

My BUILD file (/hello-world/BUILD):

load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],

    # ISSUE HERE... "Cannot create a file when that file already exists" when building
    deps = [
        "//lib/localtime:localtime",
    ],
)

wasm_cc_binary(
    name = "hello-world-wasm",
    cc_target = ":hello-world",
)

My main (/hello-world/hello-world.cc):

#include <iostream>

#include "lib/localtime/localtime.h"

int main(int argc, char** argv) {
  LocalTime::Now();
  std::cout << "hello world!" << std::endl;
  return 0;
}

/lib/localtime/localtime.h

#pragma once

namespace HelloWorld {

    class LocalTime {
        public:

        /*
        * Prints the current time to stdout
        */
        static void Now();
    };
}

/lib/localtime/localtime.cpp

#include "localtime.h"

#include <ctime>
#include <stdio.h>

namespace HelloWorld  {

    void LocalTime::Now() {
        std::time_t result = std::time(nullptr);
        printf("%s", std::asctime(std::localtime(&result)));
    }

}

/lib/localtime/BUILD

load("@rules_cc//cc:defs.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

cc_library(
    name = "localtime",
    srcs = ["localtime.cpp"],
    hdrs = ["localtime.h"]
)

Is this a bug or am I doing something wrong? Again this works great when compiling natively WITHOUT emscripten

@alienself alienself changed the title Bazel builds are always failing! [BUILD FAILURE] Bazel builds are always failing! Feb 1, 2023
@alienself alienself changed the title [BUILD FAILURE] Bazel builds are always failing! [BUILD FAILURE] Bazel builds are always failing! emsdk (3.1.31) Feb 1, 2023
@sbc100
Copy link
Collaborator

sbc100 commented Feb 1, 2023

Looks like a bug to me. We do do some basic testing as part of CI: https://github.com/emscripten-core/emsdk/blob/main/test/test_bazel.sh

Perhaps you could look at what is different about your example and the tests we run? Does this example work for you: https://github.com/emscripten-core/emsdk/blob/main/bazel/hello-world/BUILD?

@alienself alienself reopened this Feb 1, 2023
@alienself
Copy link
Author

alienself commented Feb 1, 2023

Thanks for the help @sbc100

It looks like a cache file is not being deleted or something.

If I change the name of the cc_binary it does compile for one time then making any changes to the code and recompiling causes the same issue to pop up! :(

@alienself
Copy link
Author

I fixed the issue using rm

# Remove hello-world bin (keep libs)
rm -rf ./bazel-out/wasm-*/bin/hello-world

# Build the WASM variant
bazel build hello-world:hello-world-wasm

I am going to leave this issue open because I do think this should be fixed within emscripten but at least this works

@sbc100
Copy link
Collaborator

sbc100 commented Feb 1, 2023

@walkingeyerobot @PiotrSikora @jfirebaugh who might be what is going on there. I don't use bazel myself.

@jfirebaugh
Copy link
Contributor

jfirebaugh commented Feb 1, 2023

Does it work if you name the cc_binary target "hello-world.js"? I have seen some weird behavior when the cc_binary target does not have a .js extension (#1092). Not that that should be necessary of course, just making an educated guess about where the bug might be.

@alienself
Copy link
Author

@jfirebaugh I just tried to add .js at the end the cc_binary target name but getting the same error :/

@NovaSagittarii
Copy link
Contributor

NovaSagittarii commented Dec 14, 2023

Hi, I was running into this problem as well, though adding the simd = True option in the wasm_cc_binary rule invocation resolved the "file exists" error that happened on rebuild.

I stumbled across this fix while checking the examples and there was simd = True whereas my code didn't, I added it and it just worked. I don't know why this is the case.

This didn't fix my issue and it reappeared, though it seems changing os.rename to os.replace fixes the existing file error.

os.rename(output_file, output_file + '.' + oformat)

@NovaSagittarii
Copy link
Contributor

NovaSagittarii commented Dec 27, 2023

image
https://docs.python.org/3/library/os.html#os.rename

This seems to be a Windows-only error. In Unix systems, Python's os.rename() will work regardless if the destination file exists whereas in Windows, os.rename() will fail when the destination file exists.

It is possible for the destination file to exist if it was generated previously so this issue will only come up on the second build attempt with a modification to the source files since Bazel caches previous results.

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 a pull request may close this issue.

4 participants