# Buggy code
def acr_build(
    cmd,
    client,
    registry_name,
    source_location,
    image_names=None,
    resource_group_name=None,
    timeout=None,
    build_arg=None,
    secret_build_arg=None,
    docker_file_path="Dockerfile",
    no_push=False,
    no_logs=False,
    os_type="Linux",
):
    _, resource_group_name = validate_managed_registry(
        cmd.cli_ctx, registry_name, resource_group_name, BUILD_NOT_SUPPORTED
    )

    client_registries = cf_acr_registries(cmd.cli_ctx)

    tar_file_path = os.path.join(
        tempfile.gettempdir(), "source_archive_{}.tar.gz".format(hash(os.times()))
    )

    if os.path.exists(source_location):
        if not os.path.isdir(source_location):
            raise CLIError(
                "Source location should be a local directory path or remote URL."
            )

        _check_local_docker_file(source_location, docker_file_path)

        try:
            source_location = _upload_source_code(
                client_registries,
                registry_name,
                resource_group_name,
                source_location,
                tar_file_path,
                docker_file_path,
            )
        except Exception as err:
            raise CLIError(err)
        finally:
            try:
                logger.debug(
                    "Deleting the archived source code from '%s'.", tar_file_path
                )
                os.remove(tar_file_path)
            except OSError:
                pass
    else:
        source_location = _check_remote_source_code(source_location)
        logger.warning("Sending build context to ACR...")

    if no_push:
        is_push_enabled = False
    else:
        if image_names:
            is_push_enabled = True
        else:
            is_push_enabled = False
            logger.warning(
                "'--image -t' is not provided. Skipping image push after build."
            )

    build_request = QuickBuildRequest(
        source_location=source_location,
        platform=PlatformProperties(os_type=os_type),
        docker_file_path=docker_file_path,
        image_names=image_names,
        is_push_enabled=is_push_enabled,
        timeout=timeout,
        build_arguments=(build_arg if build_arg else [])
        + (secret_build_arg if secret_build_arg else []),
    )

    queued_build = LongRunningOperation(cmd.cli_ctx)(
        client_registries.queue_build(
            resource_group_name=resource_group_name,
            registry_name=registry_name,
            build_request=build_request,
        )
    )

    build_id = queued_build.build_id
    logger.warning("Queued a build with build ID: %s", build_id)
    logger.warning("Waiting for build agent...")

    if no_logs:
        return get_build_with_polling(
            client, build_id, registry_name, resource_group_name
        )

    return acr_build_show_logs(
        client, build_id, registry_name, resource_group_name, True
    )


# Traceback (UnicodeEncodeError)
az acr build -r myreg --no-push false -t myreg.azurecr.io/repo/myapp:master -f ./Dockerfile .
Sending build context (43.398 KiB) to ACR.
Queued a build with build ID: aah
Waiting for a build agent...
time="2018-05-22T21:11:59Z" level=info msg="Running command docker login -u 00000000-0000-0000-0000-000000000000 --password-stdin myreg.azurecr.io"
Login Succeeded
time="2018-05-22T21:12:01Z" level=info msg="Untarring to /root/acr-builder/src"
time="2018-05-22T21:12:01Z" level=info msg="Running command docker build --pull -f ./Dockerfile -t myreg.azurecr.io/repo/myapp:master ."
Sending build context to Docker daemon  220.2kB

Step 1/8 : FROM node:carbon
carbon: Pulling from library/node
3d77ce4481b1: Already exists
534514c83d69: Already exists
d562b1c3ac3f: Already exists
4b85e68dc01d: Already exists
f6a66c5de9db: Already exists
7a4e7d9a081d: Already exists
49f75af57b99: Pulling fs layer
454abe4fb374: Pulling fs layer
454abe4fb374: Verifying Checksum
454abe4fb374: Download complete
49f75af57b99: Verifying Checksum
49f75af57b99: Download complete
49f75af57b99: Pull complete
454abe4fb374: Pull complete
Digest: sha256:19efc0ee886057870334b4b4b4a505ea8a158d135f592cc367ab8840b684f6c0
Status: Downloaded newer image for node:carbon
---> 037acc6a00bc
Step 2/8 : WORKDIR /usr/src/app
Removing intermediate container ade8e4293757
---> 5fa54edc3ae3
Step 3/8 : COPY package*.json ./
---> 295c9eeaf7fc
Step 4/8 : RUN npm install
---> Running in 6846edad6a09
npm WARN deprecatedERROR: 'ascii' codec can't encode character u'\U0001f64c' in position 29: ordinal not in range(128)
Traceback (most recent call last):
File "/usr/lib64/az/lib/python2.7/site-packages/knack/cli.py", line 197, in invoke
cmd_result = self.invocation.execute(args)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 332, in execute
six.reraise(*sys.exc_info())
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 306, in execute
result = cmd(params)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 167, in __call__
return super(AzCliCommand, self).__call__(*args, **kwargs)
File "/usr/lib64/az/lib/python2.7/site-packages/knack/commands.py", line 109, in __call__
return self.handler(*args, **kwargs)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/core/__init__.py", line 420, in default_command_handler
result = op(**command_args)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/command_modules/acr/build.py", line 330, in acr_build
return acr_build_show_logs(client, build_id, registry_name, resource_group_name, True)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/command_modules/acr/build.py", line 71, in acr_build_show_logs
raise_error_on_failure=raise_error_on_failure)
File "/usr/lib64/az/lib/python2.7/site-packages/azure/cli/command_modules/acr/build.py", line 132, in _stream_logs
print(flush.decode('utf-8', errors='ignore'))
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 40, in write
self.__convertor.write(text)
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 141, in write
self.write_and_convert(text)
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 166, in write_and_convert
self.write_plain_text(text, cursor, start)
File "/usr/lib64/az/lib/python2.7/site-packages/colorama/ansitowin32.py", line 174, in write_plain_text
self.wrapped.write(text[start:end])
UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f64c' in position 29: ordinal not in range(128)

# Fixed code
def acr_build(
    cmd,
    client,
    registry_name,
    source_location,
    image_names=None,
    resource_group_name=None,
    timeout=None,
    build_arg=None,
    secret_build_arg=None,
    docker_file_path="Dockerfile",
    no_format=False,
    no_push=False,
    no_logs=False,
    os_type="Linux",
):
    _, resource_group_name = validate_managed_registry(
        cmd.cli_ctx, registry_name, resource_group_name, BUILD_NOT_SUPPORTED
    )

    client_registries = cf_acr_registries(cmd.cli_ctx)

    tar_file_path = os.path.join(
        tempfile.gettempdir(), "source_archive_{}.tar.gz".format(hash(os.times()))
    )

    if os.path.exists(source_location):
        if not os.path.isdir(source_location):
            raise CLIError(
                "Source location should be a local directory path or remote URL."
            )

        _check_local_docker_file(source_location, docker_file_path)

        try:
            source_location = _upload_source_code(
                client_registries,
                registry_name,
                resource_group_name,
                source_location,
                tar_file_path,
                docker_file_path,
            )
        except Exception as err:
            raise CLIError(err)
        finally:
            try:
                logger.debug(
                    "Deleting the archived source code from '%s'.", tar_file_path
                )
                os.remove(tar_file_path)
            except OSError:
                pass
    else:
        source_location = _check_remote_source_code(source_location)
        logger.warning("Sending build context to ACR...")

    if no_push:
        is_push_enabled = False
    else:
        if image_names:
            is_push_enabled = True
        else:
            is_push_enabled = False
            logger.warning(
                "'--image -t' is not provided. Skipping image push after build."
            )

    build_request = QuickBuildRequest(
        source_location=source_location,
        platform=PlatformProperties(os_type=os_type),
        docker_file_path=docker_file_path,
        image_names=image_names,
        is_push_enabled=is_push_enabled,
        timeout=timeout,
        build_arguments=(build_arg if build_arg else [])
        + (secret_build_arg if secret_build_arg else []),
    )

    queued_build = LongRunningOperation(cmd.cli_ctx)(
        client_registries.queue_build(
            resource_group_name=resource_group_name,
            registry_name=registry_name,
            build_request=build_request,
        )
    )

    build_id = queued_build.build_id
    logger.warning("Queued a build with build ID: %s", build_id)
    logger.warning("Waiting for build agent...")

    if no_logs:
        return get_build_with_polling(
            client, build_id, registry_name, resource_group_name
        )

    return acr_build_show_logs(
        client, build_id, registry_name, resource_group_name, no_format, True
    )