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

thread 'main' panicked at 'called Result::unwrap() on an Err value: GAX(InvalidEmulatorHOST(""))', src/main.rs:15:45 #124

Closed
lasagar opened this issue Mar 28, 2023 · 12 comments

Comments

@lasagar
Copy link

lasagar commented Mar 28, 2023

thread 'main' panicked at 'called Result::unwrap() on an Err value: GAX(InvalidEmulatorHOST(""))', src/main.rs:15:45. How do i resolve this? I ran the code as is

@yoshidan
Copy link
Owner

if you are going to use emulator

  • Set OS env var SPANNER_EMULATOR_HOST to use spanner emulator.
  • Set OS env var PUBSUB_EMULATOR_HOST to use pubsub emulator

if you are not going to use emulator google_cloud_default::WithAutoExt and call with_auth for ClientConfig is required
https://github.com/yoshidan/google-cloud-rust-example/blob/547aa0f2e7ac81a09dc130617c841ba9bf4526ed/webapi/src/main.rs#L20

@lasagar
Copy link
Author

lasagar commented Mar 28, 2023 via email

@yoshidan
Copy link
Owner

Here is the google-cloud-pubsub readme.
You must use google-cloud-default together as described in the readme.
https://github.com/yoshidan/google-cloud-rust/tree/main/pubsub

I am sorry but the method of using emulator is not described in the documentation and should be clearly stated.
google-cloud-pubsub reads environment variables at the following location

let emulator = var("PUBSUB_EMULATOR_HOST").ok();

@lasagar
Copy link
Author

lasagar commented Mar 28, 2023

Hey, Thank you. How do I publish/subscribe to pubsub without using emulator. The problem i am facing is not sure how to pass the correct projectid. Following is the error. Can you pls help me with it. Thank you

Out put of Clientconfig where the project_id is none.
ClientConfig {
pool_size: Some(
4,
),
project_id: None,
environment: GoogleCloud(
,
),
endpoint: "pubsub.googleapis.com",
}

Error Message

thread 'main' panicked at 'called Result::unwrap() on an Err value: ProjectIdNotFound', src/main.rs:16:45
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

@yoshidan
Copy link
Owner

yoshidan commented Mar 29, 2023

It looks that no credential file is found.

You must set google credential file into os env 'GOOGLE_APPLICATION_CREDENTIALS' before calling with_auth.
ClientConfig.default().with_auth() reads the project_id from GOOGLE_APPLICATION_CREDENTIALS outside the GCP without emulators.

export GOOGLE_APPLICATION_CREDENTIALS=/path/your_credentials.json
let config = ClientConfig::default().with_auth().await.unwrap();
let client = Client::new(config).await.unwrap();

@lasagar
Copy link
Author

lasagar commented Mar 29, 2023

Thank you, I had set the GOOGLE_APPLICATION_CREDENTIALS, still the same. When I print the config, its showing me the project_id is None.

ClientConfig {
pool_size: Some(
4,
),
project_id: None,
environment: GoogleCloud(
,
),
endpoint: "pubsub.googleapis.com",
}
thread 'main' panicked at 'called Result::unwrap() on an Err value: ProjectIdNotFound', src/main.rs:16:45

@lasagar
Copy link
Author

lasagar commented Mar 29, 2023

Also in the client.rs, I noticed that its looking for PUBSUB_EMULATOR_HOST instead of GOOGLE_APPLICATION_CREDENTIALS.

/// ClientConfigs created by default will prefer to use PUBSUB_EMULATOR_HOST
impl Default for ClientConfig {
fn default() -> Self {
let emulator = var("PUBSUB_EMULATOR_HOST").ok();
let default_project_id = emulator.as_ref().map(|_| "local-project".to_string());
Self {
pool_size: Some(4),
environment: match emulator {
Some(v) => Environment::Emulator(v),
None => Environment::GoogleCloud(Box::new(NopeTokenSourceProvider {})),
},
project_id: default_project_id,
endpoint: PUBSUB.to_string(),
}
}
}

Is my understanding correct? Pls suggest so that i can test the publish message and subscribe message later

@yoshidan
Copy link
Owner

ClientConfig::default() is configured to use emulator, but gets the project_id from credentials by calling google-cloud-default's with_auth method.

self.project_id = ts.project_id.clone();

If the project_id is empty even though with_auth is called, then the project_id in the credentials file is empty.
I do not know what kind of credentials file you are using, but it is possible to specify the project_id directly in ClientConfig.

let mut config = ClientConfig::default().with_auth()
config.project_id = Some("your_project_id".to_string());

@lasagar
Copy link
Author

lasagar commented Mar 30, 2023 via email

@open-schnick
Copy link
Contributor

If you are still having trouble the documentation was updated in #126

@drewgatchell
Copy link

If the project_id is empty even though with_auth is called, then the project_id in the credentials file is empty.

This happens with gcloud auth login --update-adc

Credentials File ends up as:

{
  "client_id": <client_id>,
  "client_secret": <client_secret>,
  "refresh_token": <refresh_token>,
  "type": "authorized_user"
}

@yoshidan
Copy link
Owner

If the credentials does not include the project_id, the project_id must explicitly set the project_id.

let mut pubsub_config = google_cloud_pubsub::client::ClientConfig::default().with_auth().await?;
pubsub_config.project_id = Some(xxx);

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

No branches or pull requests

4 participants